How to assign a default value to a variable if it is undefined or empty. This is useful when processing a user input or if the variable value came from another command which is failing or working as unexpected.
Default setting syntax:
FINE_VARIABLE=${FINE_VARIABLE:-DEFAULT-value}
Here are some examples.
Case #1 – without assigning a value (in this case the variable is empty):
[email protected]:~$ FINE_VARIABLE=
[email protected]:~$
[email protected]:~$ echo $FINE_VARIABLE
[email protected]:~$ FINE_VARIABLE=${FINE_VARIABLE:-DEFAULT-value}
[email protected]:~$
[email protected]:~$
[email protected]:~$ echo $FINE_VARIABLE
DEFAULT-value
[email protected]:~$
Case #2 – with assigning a value (variable has some value assigned):
[email protected]:~$ FINE_VARIABLE=LetMeAssignAValue
[email protected]:~$
[email protected]:~$ FINE_VARIABLE=${FINE_VARIABLE:-DEFAULT-value}
[email protected]:~$
[email protected]:~$ echo $FINE_VARIABLE
LetMeAssignAValue
[email protected]:~$
Demo:
