Positional parameter variables in the Linux shell

Source: Internet
Author: User

To understand positional variables, first you need to know what a variable is.

A variable is a particular string that represents something that is not fixed, and simple to understand is to replace another complex or volatile data with a simple "word".

And what are positional variables?

Positional variables are the arguments that bash will pass to the script, which are stored in different positional variables, depending on their location, so that the script can invoke these variables.


Positional parameter variables:

$, $, ... : corresponding to the 1th and 2nd parameters, Shift [n] Change position
$: The command itself
$*: All arguments passed to the script, all of which are combined into a single string; represents "$1$2$3 ..."
[Email protected]: All parameters passed to the script, each of which is a separate string, representing "$" "$" "$" ... "
$#: The number of arguments passed to the script

Note: [email protected], $* only when the double quotation marks are wrapped up only when there is a difference

Example:

Write a script to test the position parameter variables above:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/DE/wKioL1etLpzQ0pAsAAAicntt0IU651.png "title=" 1.png " alt= "Wkiol1etlpzq0pasaaaicntt0iu651.png"/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/DE/wKioL1etL4_ihoOoAAAb9rwyZNM341.png "title=" 2.png " alt= "Wkiol1etl4_ihoooaaab9rwyznm341.png"/>

In this example, you can see the positional parameter variables, which are implemented in bash and are assigned different variables by location. At the time of execution of the script, the arguments are followed directly, and different variables are assigned so that they can be called directly in the script. Examples can prove that

$N (n represents a different number) and can indicate the nth parameter variable;

Note: The tenth position parameter variable should be enclosed in "{}" such as: ${10}

Example: Comparing the difference between $ $ and ${10}

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/E2/wKiom1etaGGSXahMAAAytwJ30i0261.png "title=" 3.png " alt= "Wkiom1etaggsxahmaaaytwj30i0261.png"/>

You can see the "$ 0" representation ($); ${10} "only represents the tenth positional parameter variable.

Example: Comparing the differences between "$*" and "[email protected]"

First write a test script in an empty directory, test process: 650) this.width=650; "Src=" http://s1.51cto.com/wyfs02/M00/85/E3/ Wkiom1etc8uyaehwaabfagetyr8481.png "title=" 4.png "alt=" Wkiom1etc8uyaehwaabfagetyr8481.png "/>

Can see: "Touch" $* "" created a file named "AA BB"

and "mkdir" [email protected] "" created a two directory of AA and BB.

Therefore , "$*" and "[e-mail protected]" are all parameters, but "$*" is to take all the parameters as a whole, and "[email protected]" is to take each parameter independent view .

It is important to note that $* and [email protected] differ only when they are wrapped in double quotes. let's look at the case where there are no double quotes:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/85/E2/wKioL1etdVXz-pqwAABEKhFhuV8890.png "title=" 5.png " alt= "Wkiol1etdvxz-pqwaabekhfhuv8890.png"/>

, "Touch $*" is the creation of CC and DD two files, and "mkdir [email protected]" So there is a file with the same name and not created successfully, but through the execution process can also be seen, it is to create CC and DD two directories. So when using "$*" or "[email protected]" in the future, be sure to add double quotes.


There is also a positional parameter variable of $#, which represents the number of arguments passed to the script, let's see how to use this variable.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/E3/wKioL1eteh7gyLEQAAAsJ07jSto193.png "title=" 6.png " alt= "Wkiol1eteh7gyleqaaasj07jsto193.png"/>

You can see the $ #表示位置参数的个数, and you can use "$#" to determine if there are positional parameters.

This article is from the "I ' m Groot" blog, so be sure to keep this source http://groot.blog.51cto.com/11448219/1837324

Positional parameter variables in the Linux 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.