Bash,参数列表段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash, argument list segment
提问by Mike
If you have a list in python, and you want the elements from 2 to n can do something nice like
如果您在 python 中有一个列表,并且您希望从 2 到 n 的元素可以做一些不错的事情,例如
list[2:]
I'd like to something similar with argv in Bash. I want to pass all the elements from $2 to argc to a command. I currently have
我想要与 Bash 中的 argv 类似的东西。我想将所有元素从 $2 传递给 argc 给一个命令。我目前有
command
but this is less than elegant. Would would be the "proper" way?
但这不够优雅。会是“正确”的方式吗?
回答by ghostdog74
you can do "slicing" as well, $@gets all the arguments in bash.
您也可以进行“切片”,$@在 bash 中获取所有参数。
echo "${@:2}"
gets 2nd argument onwards
从第二个参数开始
eg
例如
$ cat shell.sh
#!/bin/bash
echo "${@:2}"
$ ./shell.sh 1 2 3 4
2 3 4
回答by Ismail Badawi
Store $1somewhere, then shiftand use $@?
存储在$1某个地方,然后shift使用$@?
回答by Tom
script1.sh:
脚本1.sh:
#!/bin/bash
echo $@
script2.sh:
脚本2.sh:
#!/bin/bash
shift
echo $@
$ sh script1.sh 1 2 3 4
1 2 3 4
$ sh script2.sh 1 2 3 4
2 3 4

