#1113·shellcheck

Useless grep | awk

Author: tobbezCreated Feb 8, 2018Updated Sep 29, 2026

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

bash
#!/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:

bash
#!/bin/sh
ethtool eth0 | awk '/Speed:/ {print $2}'