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]#