Skip to content

Files

Latest commit

 

History

History
31 lines (19 loc) · 789 Bytes

SC2200.md

File metadata and controls

31 lines (19 loc) · 789 Bytes

Pattern: Use of brace expansion in [ ]

Issue: -

Description

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

Example of incorrect code:

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

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

Example of correct code:

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

Further Reading