Useless grep | awk
Author: tobbezCreated Feb 8, 2018Updated Sep 29, 2026
For new checks and feature suggestions
- shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
- I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related
Here's a snippet or screenshot that shows the problem:
#!/bin/sh
ethtool eth0 | grep 'Speed:' | awk '{print $2}'Here's what shellcheck currently says:
Nothing.
Here's what I wanted or expected to see:
A suggestion similar to SC2126 (Consider using grep -c instead of grep|wc -l) or SC2002 (Useless cat. Consider ...). Something like "Consider performing matching in awk instead of grep | awk".
The pattern seems fairly common. In most cases it's cleaner to skip the grep invocation and do the matching in awk instead. For the example above, that would be:
#!/bin/sh
ethtool eth0 | awk '/Speed:/ {print $2}'Source: koalaman/shellcheck