mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2066 (markdown)
36
SC2066.md
Normal file
36
SC2066.md
Normal file
@@ -0,0 +1,36 @@
|
||||
## Since you double quoted this, it will not word split, and the loop will only run once.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
for s in "$(mycommand)"; do echo "$s"; done
|
||||
|
||||
### Correct code:
|
||||
|
||||
The correct code depends on your intention. Let's say you're in a directory with the files `file.png` and `My cat.png`, and you want to loop over a command that outputs (or variable that contains):
|
||||
|
||||
hello world
|
||||
My *.png
|
||||
|
||||
#### Loop over each line without globbing (`hello world`, `My *.png`)
|
||||
|
||||
mycommand | while IFS= read -r s; do echo "$s"; done
|
||||
|
||||
#### Loop over each word with globbing (`hello`, `world`, `My`, `file.png`, `My cat.png`):
|
||||
|
||||
# relies on the fact that IFS by default contains space-tab-linefeed
|
||||
for s in $(mycommand); do echo "$s"; done
|
||||
|
||||
#### Loop over each line with globbing (`hello world`, `My cat.png`)
|
||||
|
||||
# explicitly set IFS to contain only a line feed
|
||||
IFS='
|
||||
'
|
||||
for s in $(mycommand); do echo "$s"; done
|
||||
|
||||
### Rationale:
|
||||
|
||||
You get this warning because you have a loop that will only ever run exactly one iteration. Since you have a loop, you clearly expect it to run more than once. You just have to decide how it should be split up.
|
||||
|
||||
### Contraindications
|
||||
|
||||
None.
|
Reference in New Issue
Block a user