Setting a default value if a bash variable is undefined

Say you want to take an optional command line argument to your script. That means that maybe $1 is set to a value, or maybe it’s not. You can read out the value and assign a default in the case that $1 is not set using the syntax:

  variable=${1:-default-value}

Which will set $variable to “default-value” if $1 is not set.

Environment Variables and Secure Programming for Linux

I read the Environment Variables section of Secure Programming for Linux and Unix HOWTO and learned about the IFS environment variable.

I also read CS 15-392 Secure Programming – Environment Variables.

The IFS environment variable is the “internal field separator” and it is typically space, tab, new line. I.e. white space used to separate fields. So in bash you can delete the IFR variable and it will default to ” \t\n” or you can set it explicitly to that value. So that explains why I found a script that unset the IFR variable — it’s a secure programming practice.