Improve command line build examples

Koichi Nakashima
2021-09-27 01:46:07 +09:00
parent c964c404d9
commit 304b92456f

@@ -44,20 +44,25 @@ Sometimes you want to split on spaces, like when building a command line:
```sh
options="-j 5 -B"
[[ $debug == "yes" ]] && options="$options -d"
make $options file
```
Just quoting this doesn't work. Instead, you should have used an array (bash, ksh, zsh):
```bash
options=(-j 5 -B) # ksh: set -A options -- -j 5 -B
options=(-j 5 -B) # ksh88: set -A options -- -j 5 -B
[[ $debug == "yes" ]] && options=("${options[@]}" -d)
make "${options[@]}" file
```
or a function (POSIX):
```sh
make_with_flags() { make -j 5 -B "$@"; }
make_with_flags() {
[ "$debug" = "yes" ] && set -- -d "$@"
make -j 5 -B "$@"
}
make_with_flags file
```