Shell

Shell

Made by DeepSource

Consider using grep -q SH-2143

Performance
Major

grep -q is a more performant way to indicate the existence of one or more matches for a pattern.

The code in the Bad Practice snippet has to iterate over the entire directory and read all matching lines into memory before making a decision. The Recommended code is cleaner and stops at the first matching line, avoiding both iterating over the rest of the directory and reading data into memory.

Bad Practice

if [ "$(find . | grep 'IMG[0-9]')" ]
then
  echo "Images found"
fi

Recommended:

if find . | grep -q 'IMG[0-9]'
then
  echo "Images found"
fi

Exceptions

The pipefail bash option may interfere with this rewrite, since the if will now be in effect, evaluating the statuses of all commands instead of just the last one. Be careful using them together.