How to have a script work with "$@" or a default list of parameters while not breaking paths with whitespace?
It appears you can't set default parameters in an expansion of ${@:-...}
, and "${@:-"$base/aaa" "$base/bbb"}"
is expanded as a single string.
If you want to set default parameters you might want to do this:
base=$(dirname -- "$0")
# test explicitly for no parameters, and set them.
if [ "$#" -eq 0 ]; then
set -- "$base/aaa" "$base/bbb"
fi
Then, the "$@"
magically quoted parameter substitution can happen unabated:
touch -- "$@"
It IS possible to use several parameters in a default expansion ${@-...}
,
like this:
#!/bin/bash
base=$(dirname "$0")
arr=("$base/aaa" "$base/bbb")
touch "${@:-"${arr[@]}"}"
But only on shells that have arrays (ksh, zsh, bash, etc.).