Replace variables in linux bash shell: = syntax, = syntax,-syntax,-syntax, =? Syntax ,? Syntax: + syntax, + syntax

Source: Internet
Author: User
Tags echo command

Replace variables in linux bash shell: = syntax, = syntax,-syntax,-syntax, =? Syntax ,? Syntax: + syntax, + syntax

Variable replacement is closely related to the default value settings, at least from the concept.

Parameter Expansion replaces a variable-like parameter with its value. For example, call a simple variable in the form of "echo $ VAR. In addition, more features can be accessed. This syntax also contains some non-Extended features, although they are meaningful. First, assign values to the default variables for such features. When using these features, the entire expression needs to be enclosed in curly brackets.

:$ {VAR: = "some default "}

The colon at the beginning of the Code is a shell command that correctly executes the inactive task. In this syntax, it only extends all parameters followed by the row. In this example, the parameter value must be extended in curly brackets.

Parameters in the ongoing colon are the most interesting part; they are logical parameter extensions in curly brackets. : = Syntax: The VAR variable will be compared with the "some defalut" string.

In this expression, if the variable VAR has not been set, the value of the expression after ": =" will be assigned to it. This value may be a number, a string, or another variable.

In the system, you may need to set multiple variables to the default value. Programmers can set default values for multiple variables in a row, instead of encoding a set of variables, which makes the code more compact and easy to read. The following example contains various replacement operations that the programmer needs to perform. The first default value is a display string, the second is a display integer, and the third is a defined variable.

:$ {VAR: = "some default" }$ {VAR2: = 42 }$ {VAR3 :=$ LOGNAME}

The replacement types of these variables are similar to the = syntax in the replacement statement. Because the syntaxes of Different replacement types are the same, but their meanings are slightly different and may be confusing. In most cases, where the replacement syntax is executed in the Code, these replicas only replace the variables with a certain value, but do not set the variables, that is, the variables are not actually assigned values. The following syntax types are defined in all shell online documents, but these descriptions are not very clear.

: =Syntax

In this replacement, use the same: = syntax as the delimiter to set the default value.

Username = ""

Echo "$ {username: = $ LOGNAME }"

When ": =" is used for comparison, the username variable has been defined, but its value is null. Therefore, the value of the variable LOGNAME is used for the echo command, that is, the value of the variable username is set to the value of LOGNAME.

With this special syntax, the variable username is set to the value of the variable LOGNAME only when the variable username has been defined and there is an actual non-null value.

The main difference between the values and values is to use the echo command instead of the passive colon to set the default value of the variable. When the activity command is called, the default value assignment is still executed and the result is displayed.

=Syntax

The following statement and: = syntax are very similar, but there is no colon.

Username = ""

Echo "$ {username = $ LOGNAME }"

As before, the variable username has been defined, but its value is null. In this syntax, the command will output the execution result of the statement after "echo. Although the variable username is null, it has been defined, so there will be no other output except one carriage return. The username variable is set to the value of the variable LOGNAME only when it is not defined at all.

This syntax is used when a script or function depends on some Defined variables. It is mainly used for login. If a specific environment variable has not been defined, you can assign it the value required by the script.

:-Syntax

In this command, because the variable username has been defined but is null, the echo statement uses the value of the LOGNAME variable.

Username = ""

Echo "$ {username:-$ LOGNAME }"

Here, the value of the username variable remains unchanged. The difference between this command and the = sentence syntax is that, before the command is executed, it is replaced only in the "$ {}" syntax in the code. That is to say, the echo command will output the value of the LOGNAME variable, but this value will not be assigned to the username variable.

-Syntax

When you delete the colon in the-statement above, that is, the value is-, the output will be blank because the username variable has been defined. If not defined, the value of the LOGNAME variable is used. The syntax is the same as-, that is, the value of the username variable has not changed.

Username = ""

Echo "$ {username-$ LOGNAME }"

When the script evaluates or checks the system environment, both-syntax and-syntax can be used. These two checks are basically the opposite. They replace the variables with the default value, or even do not depend on whether the username variable has been defined. If a set of defined variables are urgently needed in the script, and some variables that are not supposed to be defined are also required, you can combine these two syntaxes before the script executes the task to implement correct settings.

Syntax

Use :? Syntax: If the username variable has been defined as a non-null value, the username variable value will be used in the echo command. If the username variable has been defined but does not have a real value (that is, it is not empty) or is completely undefined, The LOGNAME value will be used in the echo command and the script will exit the execution.

Username = ""

Echo "$ {username :? $ LOGNAME }"

If you change the parameter of the question mark character to an incorrect character, this statement will become useful in code debugging and searching for undefined variables. This Code not only outputs strings, but also shows the position of the Code in the script.

?Syntax

From :? Remove the colon from the syntax and use the username variable does not have to be a non-null value. If username is set to only one null value, this null value is used. Conversely, if the username variable is not defined :? Syntax: Execute LOGNAME replacement, exit the script, and display the location of the code line in the script when the script exits.

Username = ""

Echo "$ {username? $ LOGNAME }"

During script debugging, check whether the variables have been defined or are not empty ,:? And? Syntax is very useful. The biggest advantage of this Code is that the script exits from the error line and displays the row number of the error line. Adding information similar to "is undefined" or "has a null value" in the text to be displayed can clearly describe the problems in the script.

: +Syntax

Compared with the preceding example, this syntax has the opposite effect. This is because the "$ {}" expression is replaced only when the variable has been defined rather than undefined.

Username = ""

Echo "$ {username: + $ LOGNAME }"

If the username variable is defined and not empty, use the value of LOGNAME. If the username variable is undefined or defined but empty, a null value is used. In any case, the value of the username variable does not change.

+Syntax

If
Colon: +. Once the variable username is defined, the "$ {}" expression uses the value of LOGNAME, the username variable does not need an actual value (that is, a non-null value ). For example

Username = ""

Echo "$ {username + $ LOGNAME }"

The syntax of ": +" and "+" is mostly the same as that of ":-" and. The main difference is that the ": +" and "+" Examples check a defined variable instead of an undefined variable. This type is similar to home method and subtraction-two sides of a coin.

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.