天天看点

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