天天看點

shell常用基本指令之七 shift

1.shift的功能

1.所有的位置參數都左移1位,即$2變$1,$3變$2

2.$# 減1

2.舉例

[[email protected] shell]# cat shift_fun.sh
#!/bin/bash 

echo "number of arguments is $#"

echo "What you input is:"
while [[ "$*" != "" ]]
do 
	echo "$1"
	shift
done
[[email protected] shell]# ./shift_fun.sh hello world
number of arguments is 2
What you input is:
hello
world
[[email protected] shell]# 
           

shift就是起到移位的作用

#!/bin/bash 

while [ "$#" -gt 0 ]
do 
	echo $*
	shift
done

運作結果:
[[email protected] shell]# ./shift_fun1.sh 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1