getting positional arguments in bash

2020-04-14 06:44发布

I have a bash script called foo with variable number of arguments, with the first one being a required one, i.e.:

foo a1 b2 b3 b4 ...

I understand that in bash $1 will get me argument a1, but is there a way to get all the rest of the arguments? $@ or $* seem to get me all the arguments including a1.

标签: bash
4条回答
对你真心纯属浪费
2楼-- · 2020-04-14 07:14

./foo.sh 1 2 3 4

#!/bin/bash
echo $1;
echo $2;
echo $3;
echo $4;

Will output:

1
2
3
4
查看更多
够拽才男人
3楼-- · 2020-04-14 07:33

Slice the $@ array.

echo "${@:2}"
查看更多
虎瘦雄心在
4楼-- · 2020-04-14 07:35
#!/bin/sh

echo $*
shift
echo $*

shift will shift all the parameters, running the previous example would give:

$ test_shift.sh a b c d e
a b c d e
b c d e
查看更多
冷血范
5楼-- · 2020-04-14 07:36

You can use shift command for that. That will remove $1 and you can access the rest of arguments starting with $1.

查看更多
登录 后发表回答