mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2201 (markdown)
26
SC2201.md
Normal file
26
SC2201.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
## Brace expansion doesn't happen in [[ ]]. Use a loop.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
[[ "$file" = index.{htm,html,php} ]] && echo "This is the main file"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
for main in index.{htm,html,php}
|
||||||
|
do
|
||||||
|
[[ "$file" = "$main" ]] && echo "This is the main file"
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Brace expansions doesn't happen in `[[ ]]`. They will just be interpreted literally.
|
||||||
|
|
||||||
|
Instead, use a `for` loop to iterate over values, and apply your condition to each.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
Reference in New Issue
Block a user