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):
dev@codetryout:~$ FINE_VARIABLE=
dev@codetryout:~$
dev@codetryout:~$ echo $FINE_VARIABLE
dev@codetryout:~$ FINE_VARIABLE=${FINE_VARIABLE:-DEFAULT-value}
dev@codetryout:~$
dev@codetryout:~$
dev@codetryout:~$ echo $FINE_VARIABLE
DEFAULT-value
dev@codetryout:~$
Case #2 – with assigning a value (variable has some value assigned):
dev@codetryout:~$ FINE_VARIABLE=LetMeAssignAValue
dev@codetryout:~$
dev@codetryout:~$ FINE_VARIABLE=${FINE_VARIABLE:-DEFAULT-value}
dev@codetryout:~$
dev@codetryout:~$ echo $FINE_VARIABLE
LetMeAssignAValue
dev@codetryout:~$
Demo: