[CONTENT] Linux Variable Default Value

中文

Content One: Set Default Values for Variables on Command Line

1.1 Assign Values to Ordinary Variables

# a=1
# b=

(Add: let a be 1 and B be null as an example)

1.2 Set Default Values for Variables on Command Line

# var1=${a:-no}
# var2=${b:-no}


Add:
If a is null, var1 is no, otherwise var1 is equal to a
If B is null, var2 is no, otherwise var2 is equal to B

1.3 View Variable Results

# echo $var1
1
# echo $var2
no


Add:
The value of a is 1, so var1 is 1
The B is null, so var2 is no

Content Two:Set Variable Defaults in Script

2.1 Setting Variable Defaults in Script

# vim var_default.sh
Create the following:
#!/bin/bash
var_default="${1:-no}"
echo $var_default

(Add:If the variable of $1 is null, then var_default is no, otherwise var_default is equal to $1, and print it out as an example)

2.2 Test Variable Defaults in Script

# bash var_default.sh
no
# bash var_default.sh 1
1
# bash var_default.sh 2
2
# bash var_default.sh 3
3