In Shell programming, you can use the Shift command to shift the value left. For example, shift 3 indicates that the original $4 is changed to $1, and the original $5 is changed to $2. The original $1, $2, and $3 are discarded, and $0 is not moved. The shift command without parameters is equivalent to shift 1.
Very useful Unix Command: shift. We know that the number of location variables or command line parameters must be fixed, or when the Shell program does not know the number, all parameters can be assigned to the variable $ * together *. If you want Shell to process parameters one by one without knowing the number of location variables, that is, after $1, after $2, it is $3. The value of $1 before the shift command is executed is not available after the shift command is executed. Example: # test shift command (x_shift.sh) until [$ #-eq 0] doecho "first parameter: $1 parameter number: $ #" shiftdone execute the above program x_shift.sh: $. the result of/x_shift.sh 1 2 3 4 is as follows: the first parameter is: 1 parameter number is: 4 the first parameter is: 2 parameter number is: 3 the first parameter is: 3. Number of parameters: 2. The first parameter is: 4. The number of parameters is: 1. We can see that the shift command is executed every time, and the number of variables ($ #) is reduced by one, the variable value is one in advance. The following code uses the until and shift commands to calculate the sum of all command line parameters. # Shift application of the previous command (x_shift2.sh) if [$ #-eq 0] thenecho "Usage: x_shift2.sh parameter "exit 1 fisum = 0 until [$ #-eq 0] dosum = 'expr $ sum + $1 'shiftdoneecho" sum is: $ sum "to execute the above program: $ x_shift2.sh 10 20 15 the result is: 45 Shift command has another important purpose. Bsh defines nine location variables, from $1 to $9, this does not mean that you can only use nine parameters in the command line. With the help of the shift command, you can access more than nine parameters. The number of moving parameters at a time is specified by the parameter included in the Shift command. For example, after the shell program processes the first nine command line parameters, you can use the shift 9 command to move $10 to $1.