Skip to content
Joachim Ansorg edited this page Nov 12, 2021 · 2 revisions

Brace expansions don't work as operands in [ ]. Use a loop.

Problematic code:

[ "$file" = index.{htm,html,php} ] && echo "This is the main file"

Correct code:

for main in index.{htm,html,php}
do
  [ "$file" = "$main" ] && echo "This is the main file"
done

Rationale:

Brace expansions in [ ] will expand to a sequence of words. Operators work on single words.

The problematic code is equivalent to [ "$file" = index.htm index.html index.php ], which is invalid syntax. A typical error message is bash: [: too many arguments or dash: somefile: unexpected operator.

Instead, use a for loop to iterate over values, and apply your condition to each.

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