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:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
for file in $dir/*.txt
|
for file in ${dir}/*.txt
|
||||||
do
|
do
|
||||||
echo "Found $file"
|
echo "Found ${file}"
|
||||||
done
|
done
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
for file in "$dir"/*.txt
|
for file in "${dir}"/*.txt
|
||||||
do
|
do
|
||||||
echo "Found $file"
|
echo "Found ${file}"
|
||||||
done
|
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.
|
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:
|
### Exceptions:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user