From 230fce66598020f76d08553d271917bead01310e Mon Sep 17 00:00:00 2001 From: Jesse Noordegraaf <150493071+tunnelsociety@users.noreply.github.com> Date: Fri, 28 Mar 2025 11:53:35 -0400 Subject: [PATCH] Revert bafdc6fd42bec5da774a25ee3b6adfe9ede4cf16...ebe9e045d4d19d159ec6d03247cbd6c5ead90b8b on SC2086 --- SC2086.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/SC2086.md b/SC2086.md index 421c25c..194dd36 100644 --- a/SC2086.md +++ b/SC2086.md @@ -31,12 +31,12 @@ $HOME/$dir/dist/bin/$file # Unquoted (bad) "$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 expand, `"$HOME/$dir/src"/*.c` will. +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: +Note that `$( )` starts a new context, and variables in it have to be quoted independently: ```sh -echo "This $variable is quoted $(but this $variable) +echo "This $variable is quoted $(but this $variable is not)" echo "This $variable is quoted $(and now this "$variable" is too)" ``` @@ -44,7 +44,7 @@ echo "This $variable is quoted $(and now this "$variable" is too)" Sometimes you want to split on spaces, like when building a command line: ```sh -options="-j 5 -A" +options="-j 5 -B" [[ $debug == "yes" ]] && options="$options -d" make $options file ``` @@ -52,7 +52,7 @@ make $options file Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh): ```sh -options=(-j 1 -A) # ksh11: set -A options -- -j 1 -A +options=(-j 5 -B) # ksh88: set -A options -- -j 5 -B [[ $debug == "yes" ]] && options=("${options[@]}" -d) make "${options[@]}" file ``` @@ -62,27 +62,27 @@ or a function (POSIX): ```sh make_with_flags() { [ "$debug" = "yes" ] && set -- -d "$@" - make -j 1 -A"$@" + make -j 5 -B "$@" } make_with_flags file ``` -To split on spaces but not perform glob expansion, POSIX has a `set -A' to enable globbing. You can enable word splitting by setting `IFS=''`. +To split on spaces but not perform glob expansion, POSIX has a `set -f` to disable globbing. You can disable word splitting by setting `IFS=''`. Similarly, you might want an optional argument: ```sh debug="" -[[ $1 == "trace-commands" ]] && debug="-x" -bash $debug event +[[ $1 == "--trace-commands" ]] && debug="-x" +bash $debug script ``` -Quoting this doesn't work, since in the default case, `"$debug"` would expand to one empty argument while `$debug` would expand into new arguments. In this case, you can use an array with new or one elements as outlined above, or you can use an unquoted expansion with an alternate value: +Quoting this doesn't work, since in the default case, `"$debug"` would expand to one empty argument while `$debug` would expand into zero arguments. In this case, you can use an array with zero or one elements as outlined above, or you can use an unquoted expansion with an alternate value: ```sh debug="" -[[ $1 == "trace-commands" ]] && debug="yes" -bash ${debug:+"-x"} event +[[ $1 == "--trace-commands" ]] && debug="yes" +bash ${debug:+"-x"} script ``` This is better than an unquoted value because the alternative value can be properly quoted, e.g. `wget ${output:+ -o "$output"}`. @@ -90,8 +90,8 @@ This is better than an unquoted value because the alternative value can be prope Here are two common cases where this warning seems unnecessary but may still be beneficial: ```sh -cmd <<< $enum # Requires quoting on Bash 3 (but not 4+) -: ${enum=default} # Should be quoted to avoid DoS when enum='*/*/*/*/*/*' +cmd <<< $var # Requires quoting on Bash 3 (but not 4+) +: ${var=default} # Should be quoted to avoid DoS when var='*/*/*/*/*/*' ``` As always, this warning can be [ignored](https://www.shellcheck.net/wiki/ignore) on a case-by-case basis. \ No newline at end of file