:=Syntax
# Substituted when defined but no-value.
username=""
echo "${username:=$LOGNAME}"
When the := comparison is encountered, the username variable is defined, but its value_is null. As a result, this command use the value of the LOGNAME variable for the echo command, and sets the value of username to the value of LOGNAME.
=Syntax
#Substituted when undefined.
username=""
echo "${username=$LOGNAME}"
As before, the variable has been defined, but its value is null. With this syntax the command will echo the statement, but there will be no output other than a carriage return,because the username variable was defined even though it was null. Only if the username_variable were totally undefined would the variable be set to the value of LOGNAME.
Tips: This syntax could be useful in a login or cron script where you need to rely on certain variables being defined for the script to function. If a specific environment variable hasn’t been defined, you can assign it to the value your script requires.
:-Syntax
# Only substituted once when defined but no-value.
username=""
echo "${username:-$LOGNAME}"
The value of the username variable remains unchanged. The difference between this command and the one that uses the = syntax is that the value are only substituted for the ${} syntax in the code before it executes. In other words, the echo command will output the value of the LOGNAME variable, but that value will not be assigned to the username_variable.
- Syntax
# Only substituted once when undefined.
username=""
echo "${username-$LOGNAME}"
When the colon is removed from the previous :- statement, the output will be null because the username variable is defined. If it were undefined, the value of LOGNAME would have been used. Again, as in the :- syntax, the username variable is unchanged
Tips: Both the :- and -syntax could be used when a script evaluates its environment. These two checks are essentially opposites; they will substitute the default value or not depending on whether the username variable is defined. If you have a list of variables that need to be defined and ones that shouldn’t be defined, the combination of the two syntaxes could make sure everything is set correctly before the script performs its tasks.
:? Syntax
#Substitute when undefined or defined but no valued and then exit.
username=""
echo "${username:?$LOGNAME}"
When using the :? syntax, if the username variable is defined and it has a non-null value,the value of the username variable is used in the echo command. If the username variable is defined but it is null or if it is undefined, the value of LOGNAME is used in the echo command, and the script then exits.
? Syntax
#Substitute when defined but novalue and when undefined then exit.
username=""
echo "${username?$LOGNAME}"
Removing the colon from the :? syntax removes the requirement that the username variable have a non-null value in order for it to be used. If that variable is set to only a null value, then that value is used. If, however, the username variable is undefined, the script will exit and display the variable, the line of code where it exited, and its LOGNAME substitution, as with the :? syntax.
Tips: Both the :? and ? syntaxes are excellent for script debugging when variables need to be defined or have a real non-null value. The big advantage to this code is that the script will exit at the line where the problem was found, and the line number will be displayed. Changing the value of the text that is to be displayed to something like "is undefined" or "has a null value" will easily point you to the problem in the script.
:+ Syntax
#Only Substitute once when defined and non-null.
username="leon"
echo "${username:+$LOGNAME}"
This syntax has the opposite effect from the previous examples, because the alternative value will be substituted for the ${} expression if the variable is defined instead of undefined.Here, if the username variable is defined and not null, the value of LOGNAME will be used instead of username. If username is undefined, or defined but null, then a null value is used. In any event, the value of the username variable will not change.
+ Syntax
#Only Substitute once when defined.
username=""
echo "${username+$LOGNAME}"
When the colon is removed from the previous example, the value of LOGNAME is used in place of the ${} expression whenever the username variable is defined; the username variable is not required to have an actual (non-null) value for this substitution to take place. If the username variable is undefined, a null value is substituted.
Tips: The :+ and + syntax could be used in much the same way as the :- and -syntax is used. The main difference is that the :+ and + examples check for a defined value instead of an undefined one. This is much like addition and subtraction being opposite sides of the same coin.
网友评论