Skip to content

Files

Latest commit

 

History

History
39 lines (27 loc) · 695 Bytes

SC2144.md

File metadata and controls

39 lines (27 loc) · 695 Bytes

Pattern: Use of glob in [/[[

Issue: -

Description

This rule warns when using globs in [/[[.

Example of incorrect code:

if [ -e dir/*.mp3 ]
then
  echo "There are mp3 files."
fi

[ -e file* ] only works if there's 0 or 1 matches. If there are multiple, it becomes [ -e file1 file2 ], and the test fails.

[[ -e file* ]] doesn't work at all.

Instead, use a for loop to expand the glob and check each result individually.

Example of correct code:

for file in dir/*.mp3
do
  if [ -e "$file" ]
  then
    echo "There are mp3 files"
    break
  fi
done

Further Reading