Summary
The Advanced Bash Scripting module is designed for cybersecurity professionals seeking advanced proficiency in scripting for security tasks and Experienced IT and system administrators focused on enhancing security automation. Throughout this module, students will develop advanced proficiency in Bash scripting techniques and enhanced ability to automate complex security tasks.
Recommended prior knowledge:
- Basic knowledge of Bash scripting and Linux/Unix systems
- Familiarity with cybersecurity concepts and practices
Command Line Arguments
If you have an opinion on what Bash Scripting is, that's good news. In this module we will try to take our Bash Scripting knowledge one step further.
Readings Arguments
On the Bash command line, arguments are the values given to the command line at the time a command or a Bash script is run. Bash enumerates these arguments like $1, $2, $3, etc. and makes them accessible. Here is more information about bash command line arguments:
-
$0: Represents the name of the command or script. -
$1,$2,$3,...: these are used to access the first second, third, etc. arguments on the command line. -
$@: Represents all command line arguments as an array. -
$#: Represents the total number of arguments given on the command line. -
$*: Represents all command line arguments as an array, but the arguments are concatenated into a single string. -
$?: Represents the exit status of the last command executed.
For example, we can print a message using command line arguments in the following script:
#!/bin/bash
echo "Arguments from command line:"
echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"
echo "All Arguments: $@"
echo "Total Arguments Count: $#"
To run this script, you can provide arguments on the command line like this:
[!bash!]$ ./arguments.sh Hello World!
In this case, the script will access the arguments given on the command line and produce the following output:

In this example, $1 gives us the first argument (Hello), $2 the second argument (World!), and $# gives us the total number of arguments.
shift
The shift command is used to shift command line arguments and change access. It is often used in conjunction with loops. Each shift command causes command line arguments to shift to the next position (dropping the previous argument). This allows us to access the arguments sequentially.
#!/bin/bash
echo "Arguments from command line:"
echo "Argument 1: $1"
shift
echo "After shifting"
echo "Argument 1: $1"

In this example, we first print the first command line argument, $1. Next, we shift the arguments one position using the shift command. The shift command shifts all arguments one position to the left, and $1 now takes the previous $2 value. Therefore, in the second echo statement, the value $1 represents the second element of the command line arguments.
Getopts
getopts is a structure used in bash script to parse command line arguments and catch certain flags or options. It is often used with a “while” loop. With getopts you can add certain flags or options to your script and have the user use those flags or options in a certain way.
#!/bin/bash
while getopts ":a:bc" opt; do
case ${opt} in
a)
echo "Option a is passed with value: $OPTARG"
;;
b)
echo "Option b is passed"
;;
c)
echo "Option c is passed"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done
As those of you who have worked in the Linux (or Unix) command line are familiar, we give some parameters to the command we will run on the Linux command line, and we do this by typing the parameter name (usually a letter) after the “-” sign. As you will notice, in this example we also had the opportunity to understand how this structure works.
In this example, we are checking three separate options using the -a, -b, and -c flags. The “getopts” loop captures each current flag as “$opt” and the value of the flag as “$OPTARG”. Then we act on certain flags with the case statement.

Finally, let's show that we can make the arguments alternative. In the example below, the user is allowed to use either "-o" or "--option" options.
This is a usage that provides comfort to the user when too many arguments are used.
#!/bin/bash
while [[ "$#" -gt 0 ]]; do
case $1 in
-o|--option)
option_value="$2"
echo "Option value: $option_value"
shift 2
;;
*)
echo "Invalid Argument: $1"
exit 1
;;
esac
shift
done