如果你有Python的list,你想从2到n的元素可以做一些漂亮的像猛砸,参数列表段
list[2:]
我想在Bash中使用的argv类似的东西。我想将所有元素从$ 2传递给argc到一个命令。我目前有
command $2 $3 $4 $5 $6 $7 $8 $9
但这不够优雅。会是“正确”的方式?
如果你有Python的list,你想从2到n的元素可以做一些漂亮的像猛砸,参数列表段
list[2:]
我想在Bash中使用的argv类似的东西。我想将所有元素从$ 2传递给argc到一个命令。我目前有
command $2 $3 $4 $5 $6 $7 $8 $9
但这不够优雅。会是“正确”的方式?
你可以做 “切割”,以及,[email protected]
得到所有的参数在bash。
echo "${@:2}"
获得第二个参数起
如
$ cat shell.sh
#!/bin/bash
echo "${@:2}"
$ ./shell.sh 1 2 3 4
2 3 4
script1.sh:
#!/bin/bash
echo [email protected]
script2.sh:
#!/bin/bash
shift
echo [email protected]
$ sh script1.sh 1 2 3 4
1 2 3 4
$ sh script2.sh 1 2 3 4
2 3 4
你也应该把它包在双引号,以避免出现问题在使用'./shell.sh 1 2“多字参数”4 5“调用''echo'$ {@:2}''时可以正常工作。 – 2010-03-06 19:08:15
值得注意的是,这也支持长度,即''$ {@:start:len}“' – Dan 2013-11-18 10:48:29
如果'$ {@:2}'为空,您还可以给出一个默认值吗? – Gabriel 2015-08-07 21:22:24