Skip to content

Instantly share code, notes, and snippets.

@tennisonchan
Created April 12, 2016 03:35
Show Gist options
  • Save tennisonchan/b3a2cf6bc4bf2192d6fbcedeaa0a5a09 to your computer and use it in GitHub Desktop.
Save tennisonchan/b3a2cf6bc4bf2192d6fbcedeaa0a5a09 to your computer and use it in GitHub Desktop.
Bash Parameter Expansion - To set default value to a variable in sh
### 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