positional parameters can be
shift shifted left with the command. For example, the
shift 3 original
$4 now become
$1 , the original
$5 now become and
$2 so on, the original
$1 ,
$2
$3 Discard,
$0 do not move. Commands with no parameters are
shift equivalent
shift 1 . a very useful Unix command: Shift. We know that for positional variables or command-line arguments, the number must be deterministic, or when the Shell program does not know its number, you can assign all parameters together to the variable $*. If the user requires that the Shell do not know the number of positional variables, but also one by one to the parameter one by one processing, that is, after $ $, after the $ $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.
examples are as follows:
#测试 Shift Command (x_shift.sh)
1 0 ]2do3"The first parameter is:" The number of arguments is: $#"4shift 5 done
Execute the above procedure x_shift.sh:
$./x_shift.sh 1 2 3 4
The results are shown below:
The first parameter is: 1 The number of parameters is: 4
The first parameter is: 2 The number of parameters is: 3
The first parameter is: 3 The number of parameters is: 2
The first parameter is: 4 The number of parameters is: 1
from the above, the shift command executes every time, the number of variables ($#) minus one, and the value of the variable is one bit ahead of the other, the following code calculates all the arguments of the command line with the until and shift commands.
#shift The application of the command on the file (x_shift2.sh)
1 if[$#-eq0 ]2 Then3Echo"Usage:x_shift2.sh Parameters"4Exit15 fi6sum=07Until [$#-eq0 ]8 Do9sum= ' expr $sum + $1`Ten Shift One Done AEcho"sum is: $sum"
Execute the above procedure:
$x _shift2.sh
it shows the result as:
$
The Shift command has another important purpose, and BSH defines 9 positional variables, from $9 to $9, which does not mean that the user can only use the command line with more than 9 parameters, and with the shift command, it is possible to access more than one or more of them.
The shift command moves the number of arguments at a time, specified by the parameters they take. For example, when the shell program finishes processing the first nine command-line arguments, you can use the Shift 9 command to move the $ $ $.
The Linux command learns the shift command