6.8 Passing arguments to a script

When running a script on the command line, we can pass arguments to it. Inside our script, we can use these arguments to e.g. write output to a specific directory or calculate something. There are pre-defined variables which we can use to access the command line arguments. $0 refers to the first argument on the command line - which is simply the name of the BASH script we’re executing. $1 refers to the second argument, $2 to the third, and so on.

For example, create a BASH script called “testArguments.sh” with the following content:

#!/bin/bash
echo "Argument 0: $0"
echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"

and execute it:

$ chmod u+x testArguments.sh
$ ./testArguments.sh is awesome
Argument 0: ./testArguments.sh
Argument 1: is
Argument 2: awesome
Argument 3: