Created Sc2045 (markdown)

koalaman
2013-11-10 11:49:09 -08:00
parent 878647f281
commit 5fb830eb38

25
Sc2045.md Normal file

@@ -0,0 +1,25 @@
# Iterate over globs whenever possible (e.g. 'for f in */*.wav'), as for loops over ls will fail for filenames like 'my file*.txt'.
### Problematic code:
for f in $(ls *.wav)
do
echo "$f"
done
### Correct code:
for f in *.wav
do
echo "$f"
done
### Rationale:
When looping over a set of files, it's always better to use globs when possible. Using command expansion causes word splitting and glob expansion, which will cause problems for certain filenames (typically first seen when trying to process a file with spaces in the name).
The following files can or will break the first loop:
touch 'filename with spaces.wav'
touch 'filename with * globs.wav'
touch 'More_Globs[2003].wav'