[Shell script learning] shift command Learning

Source: Internet
Author: User

Location parameters can be usedShift command shifts left. For exampleShift 3 indicates the original$4 now becomes$1, the original$5 now becomes$2, etc. The original$1,$2,$3 discard,$0 is not moved. Without ParametersThe shift command is equivalentshift 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.

The test command is as follows:

Root @ wl-MS-7673:/home/wl/desktop/shell # cat-n test. sh 1 # shift command (x_shift.sh) 2 until [$ #-eq 0] 3do 4 echo "the first parameter is: $1. The number of parameters is: $ #"5 shift 6doneroot @ wl-MS-7673:/home/wl/desktop/shell #
The output result is as follows:
Root @ wl-MS-7673:/home/wl/desktop/shell # chmod u + x test. sh root @ wl-MS-7673:/home/wl/desktop/shell #. /test. sh a B c d e f g the first parameter is: a parameter number: 7 the first parameter is: B parameter number: 6 the first parameter is: c parameter number is: 5. The first parameter is: d. The number of parameters is: 4. The first parameter is: e. The number of parameters is: 3. The first parameter is: f. The number of parameters is: 2. The first parameter is: g parameter number: 1root @ wl-MS-7673:/home/wl/desktop/shell #

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.