Beginners Learn linux-variable basic rules

Source: Internet
Author: User

Variables are a very important concept, both in bash scripts and in other languages. In bash, creating a variable is simple, giving the variable a name. By default, the value of the variable is empty. We can assign a value to a variable by an equal sign. It is important to note that the values of variables and variables are not a concept. When given a variable name, such as MyVar, we need to get the value of the variable by $, that is, $myvar. Use the value of the Echo output variable.

The value of the output variable is in the format: Echo $myvar or Echo ${myvar}

When using variables, the following points need to be noted:

1. Assign a value to a variable, if the value contains a space, you need to enclose it in single or double quotation marks, otherwise it will cause an error

2. When assigning a value to a variable, if single quotation marks are used, then the variables inside the single quotation mark will not parse to the true value. Use double quotation marks to

3. You can assign the result of a command execution to a variable. The format is:myvar=$ (command) or ' comand '. It is important to note that the purpose of the $ () and ${} must be separated

4. Variable amplification, the new content will be added to the original value of the variable. Format is "$ variable name" or ${variable} amplified content

5. Use export to turn variables into environment variables so that bash subprocess can use variables. Please see:

6. Determine if the variable is not set, if not set then use-the following content assignment. The syntax is: var2=${var1-hellovar1} (if Var1 is not set, the HELLOVAR1 is assigned to VAR2, otherwise the VAR1 value is assigned to VAR2). It is important to note that if VAR1 is set to NULL, then it is also assigned to a value.

7. Determine if the variable is not set or set to a null value, if it is not set or set to NULL then use-after the contents of the value. The syntax is: var2=${var1:-hellovar1}. For example, note the separation from zone 6th

For more usage and rules on 6th and 7, refer to the following table (from the bird's cousin Linux private cuisine: http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_2.php):

How variables are configured STR Not Configured Str is an empty string STR is configured not to be an empty string
VAR=${STR-EXPR} var=expr Var= Var= $str
VAR=${STR:-EXPR} var=expr var=expr Var= $str
VAR=${STR+EXPR} Var= var=expr var=expr
VAR=${STR:+EXPR} Var= Var= var=expr
VAR=${STR=EXPR} str=expr
var=expr
STR does not change
Var=
STR does not change
Var= $str
VAR=${STR:=EXPR} str=expr
var=expr
str=expr
var=expr
STR does not change
Var= $str
VAR=${STR?EXPR} Expr output to stderr Var= Var= $str
VAR=${STR:?EXPR} Expr output to stderr Expr output to stderr Var= $str

Beginners Learn linux-variable basic rules

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.