Don't use unquoted star [${*}, $*]
This rule is part of SC2048 · koalaman/shellcheck Wiki
Using unquoted star $*
in bash to pass arguments easily leads to arguments being incorrectly split up based on white space, even when quotes are used.
Here is an example, where the quoted argument "1a 1b" gets split up into separate arguments.
foo() {
local num_args="$#"
echo "Number of arguments: $num_args"
echo "arg1: ${1:-<empty>}"
echo "arg2: ${2:-<empty>}"
echo "arg3: ${3:-<empty>}"
}
# shellcheck disable=SC2016
main() {
echo.bold '"${@}"'
foo "${@}"
echo.bold '"${*}"'
foo ${*}
}
main "${@}" || exit 1
Output running the above:
❯sr "1a 1b" 2
"${@}"
Number of arguments: 2
arg1: 1a 1b
arg2: 2
arg3: <empty>
"${*}"
Number of arguments: 3
arg1: 1a
arg2: 1b
arg3: 2
Backlinks