Often times you will want to write a BASH script where you don’t want to have to keep track of all of the positional command line arguments and/or you might want to configure it with a set of environmental variables while having a default value for each in the script.
Following is the syntax for declaring them in the shell script, and then an example on how to invoke it.
echo “ARG1 = $ARG1”
echo “ARG2 = $ARG2”
ARG1 = somedefault_arg1
ARG2 = 10
$ ARG1=”someOtherArg1″ ARG2=”20″ ./default-bash-vars.sh
ARG1 = someOtherArg1
ARG2 = 20
In the example script, we have two variables, ARG1 and ARG2. When running the script without providing any additional configuration the default values will be used. When invoking it and defining the variables on the command line prior to executing the script those values will be used instead.
This prevents the situation where you potentially have many command-line arguments and then have to jugle the positional $1, $2, …. vars in the script.