Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash “如何模仿”${@:2}";in/bin/dash_Bash_Shell_Sh_Posix - Fatal编程技术网

Bash “如何模仿”${@:2}";in/bin/dash

Bash “如何模仿”${@:2}";in/bin/dash,bash,shell,sh,posix,Bash,Shell,Sh,Posix,在bash中,此语法可用于获取命令行参数列表,从$2开始: echo "${@:2}" 此语法在sh(/bin/dash)中似乎不起作用 在sh中模仿这一点的最佳方式是什么 (shift; echo "$@") 使用parens创建的子shell可以确保外部范围中的“$@”不会被修改 作为另一种方法(更丑陋,但不需要子shell),您可以删除第一个参数,然后重新添加: argOne=$1 # put $1 in $argOne shift

bash
中,此语法可用于获取命令行参数列表,从
$2开始:

echo "${@:2}"
此语法在
sh
/bin/dash
)中似乎不起作用

在sh中模仿这一点的最佳方式是什么

(shift; echo "$@")
使用parens创建的子shell可以确保外部范围中的
“$@”
不会被修改


作为另一种方法(更丑陋,但不需要子shell),您可以删除第一个参数,然后重新添加:

argOne=$1               # put $1 in $argOne
shift                   # rename $2 to $1, $3 to $2, etc
echo "$@"               # Pass the new argument list to echo
set -- "$argOne" "$@"   # Create a new argument list, restoring argOne before the rest

顺便说一句,请注意,
echo“$@”
本身就是有缺陷的,因为
echo
不区分被传递的
echo'hello world''再见world'
echo'hello''world''再见''world'
;如果细节很重要,请使用
printf'%s\n'$@'
(与POSIX兼容,但如果参数包含文字换行符,则仍然不明确),或
printf'%q\n'$@'
(仅限bash但不含糊),或
printf'%s\0'$@
(与POSIX兼容,但输出只能由添加了可读打印NUL的工具的人读取)…上述内容忽略了POSIX
echo
规范未定义其行为的所有地方。如果您的
“$@”
的第一个参数为
-n
echo
可以打印,也可以不打印;如果它包含两个字符的序列
\t
echo
可以或不可以用选项卡替换它;标准没有定义这些行为或它们的缺失。当参数不足时,我会出现此错误:
shift:cant shift似乎我只需要做一次移位?如果你只想跳过一个参数,那么你只需要移位一次。