#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.
输出:shell
-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 全部参数 $@ 或 $* 参数个数 $#bash
shift默认是shift 1 如下边为例:less
-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