Shell Script Learning Note-----Shell variables

Source: Internet
Author: User

1, in the assignment statement Name=value can not have a space, for example: name = value of the form is considered to be three variables, because essentially, the content of the script is passed to the shell program variables, and the variables are separated by a space. If you want a space in value, you need to quote it in quotation marks, and the content of the variable you want to apply is marked with a ' $ ' symbol before the variable name, as follows:

2, when we need to insert a variable between some non-whitespace characters, for example: for the extensibility of the script, often in the path name/home/my$usertxt with a variable to replace some of the content, but in this case, we can not distinguish between the variable name is user or usertxt, The solution is simply to enclose the variable name in curly braces, such as/home/my${user}txt.

3, when defining a variable, add export to the variable name before it can be changed into a global variable, and can be called in the child shell, but it should be noted that the global variable is passed through the "value of" the child shell, similar to the C language function parameters, Although its value has been changed in the child function, its original value has not changed. So is there something like a pointer that can directly change the value of the original variable? Answer yes, no. (All global variables can be viewed via env or EXPORT-P command)

4, at the same time we can be like C language directly to the shell script to pass the command line parameters, in the script to represent the first parameter, the second argument, and so on, it should be noted that when the tenth parameter is represented, you need to enclose the ordinal with curly braces, for example: ${10}, because $ 10 will be broken down to $0 and $. It is important to note that if the command line argument represents a string with spaces, then the input command line is to enclose the argument in quotation marks, while in the script reference, also use quotation marks, for example: ls "${1}", otherwise within the script, this parameter will also be mistaken for a space by a number of parameters

5, in the script, the symbol $* represents all the command line parameters, we can use the following way to traverse it, but usually we prefer to use "[email protected]", because it can be in the command line parameters within the space when it distinguishes it

6. Variable $ #用于统计命令行参数的个数:

7, the default value of the variable: 1) b=${a:-"xxx"} means if the variable a is not set or the value is empty, then b= "xxx", 2) with:-The symbol is different ${a:= "xxx"} means if a is not set or is empty, then a is set to ' xxx ' and return the value "XXX", you can think :-Do only return values, not assignments. 3) ${a= "xxx"} means that if and only if the variable A is not set, its value is "xxx", which means to allow the default value of the variable name to be null

8. The definition and use of array variables are as follows:

9, finally, in fact, when the variable assignment, if the use of double quotation marks, then the double quotation marks can be more than just a string, the shell will actually parse the content inside the quotation marks. For example, the $home in quotation marks will be replaced with the value of the variable home, $ (PWD) will be replaced with the output of the command pwd, $ (()) will be replaced with the result of the operation

References: "Bash Cookbook", Carl albing

Shell Script Learning Note-----Shell variables

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.