#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