diff --git a/Sc2086.md b/Sc2086.md index 786fd75..831fd8c 100644 --- a/Sc2086.md +++ b/Sc2086.md @@ -9,7 +9,15 @@ ### Rationale The problematic code looks like "print the first argument". It's actually "Split the first argument by spaces, tabs and line feeds. Expand each of them as if it was a glob. Join all the resulting strings and filenames with spaces. Print the result." -Quoting prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such. +Quoting variables prevents word splitting and glob expansion, and prevents the script from breaking when input contains spaces, line feeds, glob characters and such. + +Strictly speaking, only expansions themselves need to be quoted, but for stylistic reasons, entire arguments with multiple variable and literal parts are often quoted as one: + + $HOME/$dir/dist/bin/$file # Unquoted (bad) + "$HOME"/"$dir"/dist/bin/"$file" # Minimal quoting (good) + "$HOME/$dir/dist/bin/$file" # Canonical quoting (good) + +When quoting composite arguments, make sure to exclude globs and brace expansions, which lose their special meaning in double quotes: `"$HOME/$dir/src/*.c"` will not expand, but `"$HOME/$dir/src"/*.c` will. Note that `$( )` starts a new context, and variables in it have to be quoted independently: