Pattern: Looping over single, constant value
Issue: -
This rule checks for loops that iterate over a single, constant value. This is most likely a bug in your code, caused not expanding the value in the way you want.
You should make sure that whatever you loop over will expand into multiple words.
Example of incorrect code:
for var in value
do
echo "$var"
done
Example of correct code:
Correct code depends on what you want to do.
To iterate over files in a directory, instead of for var in /some/dir
use:
for var in /some/dir/* ; do echo "$var"; done
To iterate over lines in a file or command output, use a while read loop instead:
somecommand | while IFS= read -r line; do echo "$line"; done
To iterate over words written to a command or function's stdout, instead of for var in somefunction
, use
for var in $(somefunction); do echo "$var"; done
To iterate over words in a variable, instead of for var in somevariable
, use
for var in $somevariable; do echo "$var"; done