Skip to content
Vidar Holen edited this page Jan 21, 2015 · 9 revisions

-e doesn't work with globs. Use a for loop.

Problematic code:

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

Correct code:

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

Rationale:

[ -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.

Exceptions

None.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally