Updated SC2086 (markdown)

koalaman
2016-09-05 16:49:09 -07:00
parent 2579e70882
commit cb0e8deb6e

@@ -40,7 +40,7 @@ echo "This $variable is quoted $(and now this "$variable" is too)"
``` ```
### Exceptions ### Exceptions
Sometimes you want to split on spaces, like when building a command line. Sometimes you want to split on spaces, like when building a command line:
```sh ```sh
options="-j 5 -B" options="-j 5 -B"
@@ -63,3 +63,20 @@ 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 -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 script
```
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"} script
```
This is better than an unquoted value because the alternative value can be properly quoted, e.g. `wget ${output:+ -o "$output"}`.