在允许变量赋值中的 ${@:2} 语法时,他们说我不应该使用"${@:2}"
它,因为它会破坏不同外壳的东西,我应该使用它"${*:2}"
。
但是使用"${*:2}"
代替"${@:2}"
是无意义的,因为做"${@:2}"
不等于"${*:2}"
下面的例子:
#!/bin/bash
check_args() {
echo "\$#=$#"
local counter=0
for var in "$@"
do
counter=$((counter+1));
printf "$counter. '$var', ";
done
printf "\\n\\n"
}
# setting arguments
set -- "space1 notspace" "space2 notspace" "lastargument"; counter=1
echo $counter': ---------------- "$*"'; counter=$((counter+1))
check_args "$*"
echo $counter': ---------------- "${*:2}"'; counter=$((counter+1))
check_args "${*:2}"
echo $counter': ---------------- "${@:2}"'; counter=$((counter+1))
check_args "${@:2}"
-->
GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
1: ---------------- "$*"
$#=1
1. 'space1 notspace space2 notspace lastargument',
2: ---------------- "${*:2}"
$#=1
1. 'space2 notspace lastargument',
3: ---------------- "${@:2}"
$#=2
1. 'space2 notspace', 2. 'lastargument',
如果我不能使用"${@:2}"
(如他们所说),我可以使用什么等价物?
这是原始问题处理除第一个参数以外的所有参数(在 bash 脚本中),他们将参数与空格保持在一起的唯一答案是使用"${@:2}"