Default parameters in bash

Often you want to use default values in a bash script. Mostly when you want to make use of optional arguments. You can easily achieve that with parameter substitution.

unset VAR
VAR=${VAR-default}
echo $VAR        # VAR is "default"

VAR=${VAR-another default}
echo $VAR        # VAR is still "default"

There are more parameter substitutions which you can checkout at http://tldp.org/LDP/abs/html/parameter-substitution.html

Leave a comment

Your email address will not be published. Required fields are marked *