mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
let's eat our own—SC2250—dog food
12
SC2231.md
12
SC2231.md
@@ -1,20 +1,20 @@
|
||||
## Quote expansions in this `for` loop glob to prevent wordsplitting, e.g. `"$dir"/*.txt` .
|
||||
## Quote expansions in this `for` loop glob to prevent word splitting, e.g. `"${dir}"/*.txt`.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
```sh
|
||||
for file in $dir/*.txt
|
||||
for file in ${dir}/*.txt
|
||||
do
|
||||
echo "Found $file"
|
||||
echo "Found ${file}"
|
||||
done
|
||||
```
|
||||
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
for file in "$dir"/*.txt
|
||||
for file in "${dir}"/*.txt
|
||||
do
|
||||
echo "Found $file"
|
||||
echo "Found ${file}"
|
||||
done
|
||||
```
|
||||
|
||||
@@ -22,7 +22,7 @@ done
|
||||
|
||||
When iterating over globs containing expansions, you can still quote all expansions in the path to better handle whitespace and special characters.
|
||||
|
||||
Just make sure glob characters are outside quotes. `"$dir/*.txt"` will not glob expand, but `"$dir"/*.txt` or `"$dir"/*."$ext"` will.
|
||||
Just make sure glob characters are outside quotes. `"${dir}/*.txt"` will not glob expand, but `"${dir}"/*.txt` or `"${dir}"/*."${ext}"` will.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
|
Reference in New Issue
Block a user