Define default values for bash scrip arguments

Note to self: bash is cool!!!!

I was made aware that you can define a default value for a variable in the following manner:


So if parameter is unset or null, the expression is replaced with the expansion of ‘word’. I went searching the bash man page for more information and it has lots of other goodies:

${parameter:-word} : word replaces the expression when parameter is unset or null.
${parameter:=word}: word is assigned to parameter when parameter is unset or null.
${parameter:?word}: word is output to stderr if parameter is unset or null.
${parameter:+word}: replace parameter with word when parameter is null or unset.
${parameter:offset:length}: A substring of parameter is expanded.

For more specifics of how these work check out the bash man page under parameter expansion section.


About joelgranados

I'm fascinated with how technology and science impact our reality and am drawn to leverage them in order to increase the potential of human activity.
This entry was posted in commands and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s