Script Arguments
(some) BASH special variables
When a script is executed, a number of variables are created by the interpreter: probably the more important for what matters to us are $#
, $0
and $1
, $2
, ... (for more info. see "Special Parameters" in the BASH manpage).
The first one holds the number of parameters passed to our script, $0
will be set automatically to the name of our script and the other ($1
, $2
, ...) will contain the parameters passed to it (if any).
An example of usage of such parameters in our script is in line 5, where we assign the content of the first parameter passed to the (newly created) variable "output_file":
output_file=$1
If no parameters are passed to the script, then an empty string will be passed to output_file.
$0
is sometime useful to print nice "usage messages": since it's value is always the name of the script the user is executing, therefore a command like this:
echo -e 'Usage: $0 [optional args] filename'
will always show to the user the correct name of the script, even if he/she has renamed it.