From e2774d798acb167341be3e165a9e0624bd8d88b4 Mon Sep 17 00:00:00 2001 From: koalaman Date: Thu, 8 May 2014 19:22:55 -0700 Subject: [PATCH] Updated Sc2045 (markdown) --- Sc2045.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sc2045.md b/Sc2045.md index bb0414d..b8970ce 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'. +## 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: @@ -14,6 +14,8 @@ 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) + ### 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). @@ -23,4 +25,4 @@ 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' - touch 'files_with_fønny_chæracters_in_certain_locales.wav' \ No newline at end of file + touch 'files_with_fønny_chæracters_in_certain_locales.wav'