Assigning a Variable Default Value in Bash
Bash allows you to specify a default value for any variable when it is declared or assigned. The cool part is you can do it on the same line.
💎 Simple things like this really add up when duct taping something together to provide an immediate solution. More formal languages don’t have these little things because they exist to provide more permanent solutions. This is why it is so crucial that you understand and accept the idea of initial prototyping and the languages that are best at it. Always use the right tool for the job.
Generally you need one of the following:
- assign a new variable the value of another or a default if empty, or
- change the content of an existing variable to a default if it is empty.
Usually you want the first. Use the
You can obviously use that for the second case as well:
But there is another way you will see floating around out there.
: is a non-operator so it just allows the line to exist.
🤢 I really don’t like this method because it is unnecessarily esoteric and just another thing to remember. I suggest avoiding the
:non-operator whenever possible.