如何在 /bin/dash 中模拟“${@:2}”
how to emulate "${@:2}" in /bin/dash
在bash
中,此语法可用于获取命令行参数列表,从</code>:</p>开始
<pre><code>echo "${@:2}"
此语法似乎不适用于 sh
(/bin/dash
)。
在 sh 中模拟它的最佳方法是什么?
(shift; echo "$@")
使用 parens 创建的子 shell 确保外部作用域中的 "$@"
不被修改。
作为另一种方法(更丑陋,但避免了对子 shell 的需要),您可以删除第一个参数,稍后重新添加它:
argOne= # put in $argOne
shift # rename to , to , etc
echo "$@" # Pass the new argument list to echo
set -- "$argOne" "$@" # Create a new argument list, restoring argOne before the rest
在bash
中,此语法可用于获取命令行参数列表,从</code>:</p>开始
<pre><code>echo "${@:2}"
此语法似乎不适用于 sh
(/bin/dash
)。
在 sh 中模拟它的最佳方法是什么?
(shift; echo "$@")
使用 parens 创建的子 shell 确保外部作用域中的 "$@"
不被修改。
作为另一种方法(更丑陋,但避免了对子 shell 的需要),您可以删除第一个参数,稍后重新添加它:
argOne= # put in $argOne
shift # rename to , to , etc
echo "$@" # Pass the new argument list to echo
set -- "$argOne" "$@" # Create a new argument list, restoring argOne before the rest