理解Bash shell中shift命令

1. man下的解释:

[root@desktop31 log]# man shift

...

       shift [n]
              The  positional  parameters  from n+1 ... are renamed to $1 ....
              Parameters represented by the numbers  $#  down  to  $#-n+1  are
              unset.   n  must  be a non-negative number less than or equal to
              $#.  If n is 0, no parameters are changed.  If n is  not  given,
              it  is assumed to be 1.  If n is greater than $#, the positional
              parameters are not changed.  The return status is  greater  than
              zero if n is greater than $# or less than zero; otherwise 0.

...

shift n表示把第n+1个参数移到第1个参数, 即命令结束后$1的值等于$n+1的值, 而命令执行前的前面n个参数不能被再次引用, 后面$#-n+1到$#的参数被unset, 参数的个数减少为$#-n个.

n的值不能为负数, 若n为0或大于参数个数$#则参数不变, 若n没有给定则默认为1. 当n小于0或者大于参数个数$#时shift命令的返回值大于0, 否则返回0.


2. 小例子

[root@desktop31 log]# vim test

#!/bin/bash
echo '>> before shift '
echo 'para count is ' $#
echo '$1 2 3 is ' $1, $2, $3.
shift 2
echo '>> after shift 2'
echo 'para count is ' $#
echo '$1 2 3 is ' $1, $2, $3.


[root@desktop31 log]# ./test a b c
>> before shift
para count is  3
$1 2 3 is  a, b, c.
>> after shift 2
para count is  1
$1 2 3 is  c, , .
[root@desktop31 log]#


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值