From ebe9e045d4d19d159ec6d03247cbd6c5ead90b8b Mon Sep 17 00:00:00 2001 From: owshiiii <201320587+ram-skrrrtskrrrt@users.noreply.github.com> Date: Thu, 20 Mar 2025 04:36:39 +0800 Subject: [PATCH] Updated SC2086 (markdown) --- SC2086.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/SC2086.md b/SC2086.md index 194dd36..421c25c 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 not expand, but `"$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 expand, `"$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 is not)" +echo "This $variable is quoted $(but this $variable) 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 -B" +options="-j 5 -A" [[ $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 5 -B) # ksh88: set -A options -- -j 5 -B +options=(-j 1 -A) # ksh11: set -A options -- -j 1 -A [[ $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 5 -B "$@" + make -j 1 -A"$@" } make_with_flags file ``` -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=''`. +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=''`. Similarly, you might want an optional argument: ```sh debug="" -[[ $1 == "--trace-commands" ]] && debug="-x" -bash $debug script +[[ $1 == "trace-commands" ]] && debug="-x" +bash $debug event ``` -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: +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: ```sh debug="" -[[ $1 == "--trace-commands" ]] && debug="yes" -bash ${debug:+"-x"} script +[[ $1 == "trace-commands" ]] && debug="yes" +bash ${debug:+"-x"} event ``` 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 <<< $var # Requires quoting on Bash 3 (but not 4+) -: ${var=default} # Should be quoted to avoid DoS when var='*/*/*/*/*/*' +cmd <<< $enum # Requires quoting on Bash 3 (but not 4+) +: ${enum=default} # Should be quoted to avoid DoS when enum='*/*/*/*/*/*' ``` 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