shift

#This is a BASH shell builtin, to display your local syntax from the bash prompt type: help shift
-bash-4.1$ help shift
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 $#.

例子

#!/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.

输出:

-bash-4.1$ sh 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, , .

shift可以用来向左移动位置参数。Shell的名字 $0第一个参数 $1第二个参数 $2第n个参数 $n所有参数 $@ 或 $*参数个数 $#

shift默认是shift 1以下边为例:

-bash-4.1$ cat shift.sh
#!/bin/bash
until [ -z "$1" ]
do
  echo "$@"
  shift
done


-bash-4.1$ sh shift.sh 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9
09-06 02:17