在替换参数值后,您需要双引号$@
以防止 bash 执行不需要的解析步骤(分词等):
function Foobar
{
cmd -opt1 -opt2 "$@"
}
从 bash 手册页的特殊参数部分编辑:
@ Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$@" is equivalent to "$1"
"$2" ... If the double-quoted expansion occurs within a word,
the expansion of the first parameter is joined with the begin-
ning part of the original word, and the expansion of the last
parameter is joined with the last part of the original word.
When there are no positional parameters, "$@" and $@ expand to
nothing (i.e., they are removed).