Bash Shell Scripting Part 5 – Arguments

Bash Shell ScriptingArguments bring up thoughts of heated exchanges, differences of opinion, and persuasive reasoning — unless you’re a programmer.

A programmer knows that an argument is actually a value or data that is passed to a function or a program when it is called.

In fact, if you’ve even so much as used the command line, and typed in various commands, you have no doubt used arguments with those commands already.

In bash shell scripts, arguments can be added to the command line when a script is executed, and those arguments can be read/accessed within the script.

Special variables are used in shell scripts to access command line arguments, so to begin with, let’s take a look at the special variables available.

Special Variables

The Linux bash shell has a number of special parameters reserved for specific functions.

Many of those same special parameters can be used in bash shell scripts as special variables.

$$ – Refers to the PID (process ID) of the current shell or bash script.
$? – Refers to the exit status of the last executed foreground command.
$! – Refers to the PID (process ID) of the last command run in the background.
$0 – Refers to the name of the shell script itself.
$n – Where n points to the nth argument. (See positional parameters.)
$* – Refers to all positional parameters (arguments) in a single string, comma separated.
$@ – Refers to all positional parameters in a single string, separated by spaces.
$# – Refers to the number of positional parameters (arguments) specified.
$- – Refers to the options specified in the shell.
$_ – Refers to the absolute file name of the shell or the bash script.

Positional Parameters

Positional parameters are used to access command line arguments that are passed to bash shell scripts.

Positional parameters are reserved variables $1 – $9 ($1, $2, $3, $4, $5, $6, $7, $8 and $9) that correspond with arguments entered into the command line when a shell script is executed.

Additional positional parameters — not supported by all shells — are available when enclosed in brackets: ${10}, ${11}, etc.

Special variables can be used to determine if positional parameters have been passed to the script, as well as how many, etc.

Let’s take a look at an example of a script making use of positional parameters and a few other special variables as well.

#!/bin/bash
let add=$1+$2
echo $add
echo "$# arguments were entered."
exit 0

In this example, the let command is used to add together the first two arguments, and the resulting variable contents are echoed, along with a report of how many arguments were passed to the script at the time of its execution.

The script, when executed, should then include two or more arguments: ./addition.sh 8 79 or ./addition.sh 4 2 6 1

Conclusion

In later lessons, we will explore scenarios such as how to test for the presence of arguments, and what to do with each argument depending on how many are encountered, or even what to do based on each argument’s values.

In the meantime, practice a bit with what you can already do with arguments passed to the shell script, and let your imagination run wild.

Leave a Reply

You have to agree to the comment policy.