Created
April 12, 2016 03:35
-
-
Save tennisonchan/b3a2cf6bc4bf2192d6fbcedeaa0a5a09 to your computer and use it in GitHub Desktop.
Bash Parameter Expansion - To set default value to a variable in sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### variable doesn't exist | |
echo $variable1 | |
variable1="${variable1:-default value}" | |
echo $variable1 | |
# return default value | |
### variable exists | |
variable2="a" | |
echo $variable2 | |
# reture a | |
variable2="${variable2:-default value}" | |
echo variable2 | |
#return a | |
### using a variable as default | |
echo $variable3 | |
default="defalut value" | |
variable3="${variable3:-$default}" | |
echo $variable3 | |
# return default value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment