diff --git a/Sc2045.md b/Sc2045.md index a4588f3..fe32c16 100644 --- a/Sc2045.md +++ b/Sc2045.md @@ -1,4 +1,4 @@ -## Iterate over globs whenever possible (e.g. 'for f in */*.wav'), as for loops over ls will fail for filenames like 'my file*.txt'. +## Iterating over ls output is fragile. Use globs. ### Problematic code: @@ -11,10 +11,11 @@ for f in *.wav do + [[ -e $f ]] || break # handle the case of no *.wav files echo "$f" done -(Also note that in Bash, `shopt -s nullglob` will allow the loop to run 0 times instead of 1 if there are no matches. There are [several other conditions](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) to look out for. ) +Also note that in Bash, `shopt -s nullglob` will allow the loop to run 0 times instead of 1 if there are no matches. There are also [several other conditions](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) to be aware of. ### Rationale: