Linux shell-shift Command Usage (reprint)

Source: Internet
Author: User

Position parameters can be usedShiftcommand to move left. Like whatShift 3said the original $4 now become $ $ , the original $ $ now becomes $ $, etc., the original $, $, $ discarded, don't move. The shift command with no parameters is equivalent to shift 1.

It's very useful.UnixCommand: Shift。 We know that for positional variables or command-line arguments, the number must be deterministic, or whenShellWhen the program does not know its number, you can assign all parameters together to the variable$*。 If the user requests Shell In the case of not knowing the number of positional variables, you can also handle the parameter one by one one by one, that is, in $2, in $ $ shift command execution pre-variable value in shift command is not available after execution.

Examples are as follows:

# Test Shift Command (x_shift.sh)

Until [$#-eq 0]

Do

echo "The first parameter is : the number of arguments is : $#"

Shift

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)

If [$#-eq 0]

Then

echo "Usage:x_shift2.sh parameter "

Exit 1

Fi

Sum=0

Until [$#-eq 0]

Do

sum= ' expr $sum + $ '

Shift

Done

echo "sum is: $sum"

Execute the above procedure :

$x _shift2.sh 10 20 15

It shows the result as:

45

shift The order has another important use Bsh defines 9 positional variables, from to $9,9 shift command can access more than 9

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 $ $ $.

From

Linux shell-shift Command Usage (reprint)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.