Shell script learning notes ----- shell variables, learning notes ----- shell

Source: Internet
Author: User

Shell script learning notes ----- shell variables, learning notes ----- shell

1. There cannot be spaces in the value assignment statement name = value. For example, the form of name = value is considered to be three variables, because, in essence, the content of the script is the variables passed to the shell program, and the variables are differentiated by spaces. If you want a space in the value, you need to mark it with quotation marks. To apply the variable content, add the '$' symbol before the variable name, as shown below:

2. When we need to insert variables between some non-space characters, for example, for the scalability of the script, some content is often replaced by variables in the path name/home/my $ USERtxt, but in this case, we can't tell whether the variable name is USER or USERtxt. The solution is actually very simple. Enclose the variable name in curly brackets, for example,/home/my $ {USER} txt

3. When defining a variable, you can add an export before the variable name to change the variable to a global variable and call it in the sub-shell. However, note that, the global variable enters the sub-shell through "value transfer", similar to function parameters in C language, although its value is changed in the sub-function, but its original value has not changed. Is there anything like a pointer that can directly change the value of the original variable? The answer is no. (You can use the env or export-p command to view all global variables)

4. At the same time, we can directly PASS command line parameters to the shell script like the C language. In the script, $1 indicates the first parameter, $2 indicates the second parameter, and so on, it should be noted that when it represents more than 10 parameters, You need to enclose the serial number with curly brackets, for example: ${10 }, because $10 is divided into $1 and 0. Note that if the string represented by the command line parameter contains spaces, you must enclose the parameter in quotation marks when you input the command line parameter and reference it in the script, use quotation marks, for example, ls "$ {1}". Otherwise, the parameter is mistakenly considered as a multiple parameter separated by spaces.

5. In the script, the symbol $ * indicates all the command line parameters. We can traverse the parameters as follows. However, we generally recommend that you use "$ @", because it can be distinguished when there are spaces in the command line parameters.

6. Variable $ # used to count the number of command line parameters:

7. Default Value of the variable: 1) B =$ {A:-"xxx"} indicates that if variable A is not set or the value is null, B = "xxx", 2) unlike the-symbol, $ {A: = "xxx"} indicates that if A is not set or is empty, A is set to 'xxx' and the return value is "xxx ", you can think:-only return values, without assigning values. 3) $ {A = "xxx"} indicates that when and only when variable A is not set, its value is set to "xxx ", this means that the default value of the variable name is allowed to be null.

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

9. In the end, if double quotation marks are used during variable assignment, the double quotation marks can be more than strings. In fact, shell will parse the content in the quotation marks. For example, $ 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, $(1 + 1 )) is replaced with the result of the 1 + 1 operation.

 

References: bash Cookbook, Carl Albing

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.