shell中shift的用法

本文详细介绍了bash脚本中Shift命令的使用方法及应用场景。通过实际示例演示了如何利用Shift命令进行参数位置的调整,适用于初学者及有经验的脚本开发者。
#!/usr/bin/env bash
# 测试shift的用法
# 也就是说,这个命令的主要作用就是参数位置重命名的,shift和shift 1效果一样
# 把$N+1, $N+2, ... 替换为$!, $2, ,,,,这里的N其实就是shift后面跟的参数,默认是1,因为$0是文件名本身,shift 2就相当于把原来的$2+1替换为$2,以此类推
:<<helpMsg
shift: shift [n]
    Shift positional parameters.

        Rename the positional parameters $N+1,$N+2 ... to $1,$2 ...  If N is
            not given, it is assumed to be 1.

    Exit Status:
            Returns success unless N is negative or greater than $#.
helpMsg

# 执行结果如下:
:<<EOF
 # sh test.sh 1 2 3 4 5 6 7 8 9
 1 2 3 4 5
 test.sh 2 3 4 5 6
 test.sh 3 4 5 6 7
 test.sh 5 6 7 8 9
EOF

:<<test2
]# sh test.sh 1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5
test.sh 2 3 4 5 6
test.sh 3 4 5 6 7
test.sh 5 6 7 8 9
test.sh 9 10 11 12
test2

echo $1 $2 $3 $4 $5
shift 1
echo $0 $1 $2 $3 $4 $5
shift
echo $0 $1 $2 $3 $4 $5
shift 2
echo $0 $1 $2 $3 $4 $5
shift 4
echo $0 $1 $2 $3 $4 $5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值