您的位置:首页 > 运维架构 > Shell

shell常用基本命令之七 shift

2016-05-27 15:41 465 查看

1.shift的功能

1.所有的位置参数都左移1位,即$2变$1,$3变$2

2.$# 减1

2.举例

[root@nn shell]# cat shift_fun.sh
#!/bin/bash

echo "number of arguments is $#"

echo "What you input is:"
while [[ "$*" != "" ]]
do
echo "$1"
shift
done
[root@nn shell]# ./shift_fun.sh hello world
number of arguments is 2
What you input is:
hello
world
[root@nn shell]#
shift就是起到移位的作用
#!/bin/bash

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

运行结果:
[root@nn 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell shift