Updated Sc2086 (markdown)

koalaman
2015-07-02 19:55:27 -07:00
parent 64929ecb6a
commit bbdf114f12

@@ -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: