访问从给定索引传递给shell脚本的所有variables

如何从第三个开始访问$@的值? 现在我把它们从三个传递到九个,但我认为这是一个更好的方法:

 while getopts ":n" opt "$3 $4 $5 $6 $7 $8 $9"; do 

看起来像一个时髦的方法来论证我,但:

 [kbrandt@kbrandt: ~/scrap] cat args args=("$@") echo ${args[0]} echo ${args[@]:1:2} echo ${args[@]:0:$#} [kbrandt@kbrandt: ~/scrap] bash args foo bar baz biz foo bar baz foo bar baz biz 

我build议你查看关于命令行参数的常见问题解答 (基本上说getopts或loop / case / shift)。

我假设你在这种情况下使用bash?

如果是这样,你应该使用移位 。

一个例子:

shift.sh的内容:

 #!/bin/bash shift 3 echo $* 

结果:

 graeme@graeme:~$ ./shift.sh one two three four five six four five six