Bash scripts are simple text files that contain a collection of commands. Bash scripts can help with administrative tasks, task automation, and executing multiple commands. They are used to automate recurring tasks/actions.
We can put all the commands that run on the terminal into a bash script and vice versa. Bash Scripts include imperative programming concepts like loops, conditionals, and functions.
Command-Line Arguments are parameters that are specified with the filename of the bash script at the time of execution. The command-line arguments allow the script to perform dynamic actions based on the input:
How to Pass an Argument to a Shell Script
To pass a parameter to a bash script just write it after the name of the bash script in the terminal:
./my-test-script.sh argument
How to Pass Multiple Arguments to Shell Script
You can also specify multiple arguments separated by space along with the name of the bash script file:
./my-test-script.sh arg_1 arg_2 arg_3............. arg_n
We can use the predefined variables to recall these arguments in the bash script. The first argument can be recalled by $1
, the second by $2
, and so on. The pre-defined variable “$0”
refers to the bash script itself. The list of some other important predefined variables is given below:
$@
: Values of all arguments$#
:Total number of arguments$$
: Process ID of the current shell
Now we will use a bash file named animals.sh as an example.
1 2 3 4 5 6 7 | #!/bin/bash echo "The animal in the first enclosure is: $1" echo "The animal in the second enclosure is: $2" echo "The animal in the third enclosure is: $3" echo "The total number of animals in the zoo are: $#" echo "The names of all the animals are: $@" |
Now we will run this script in the terminal:
How to Pass an Argument Containing Space
If an argument consists of multiple words that are separated by space then we need to enclose it in single quotes to pass it as a single argument:
1 2 3 | #!/bin/bash echo "My name is: $1" |
How to Pass Arguments with Special Characters
We can also pass special characters as arguments. But they need to be written with backslashes before them.
1 2 3 | #!/bin/bash echo "How much money did you spend today: $1" |
Passing Arguments Using Flags and Options in Shell Script
Using flags and options is another common way of giving input to a bash script. An option is always followed by a value while flags are not followed by any value.
First, we will make a new bash script that takes two different arguments (options) i.e. -n/--name
for name, and -i/--id
for an identification number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #!/bin/bash ARGS=$(getopt -a --options n:i: --long "name:,id:" -- "$@") eval set -- "$ARGS" while true; do case "$1" in -n|--name) name="$2" echo "Name: $name" shift 2;; -i|--id) identification="$2" echo "ID: $identification" shift 2;; --) break;; esac done |
In the code given above first, we created a variable that stores all the short and long names of our options. On the second line, we evaluated the ARGS
variable. Then we used a while loop to assign a block of code to each option.
In the code above shift is used to skip the values of the variables. The digit that follows the shift defines how many variables are skipped.
Similarly
Now we will add a -v
flag which prints out a verbose menu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/bin/bash ARGS=$(getopt -a --options n:i:v --long "name:,id:,verbose" -- "$@") eval set -- "$ARGS" while true; do case "$1" in -n|--name) name="$2" echo "Name: $name" shift 2;; -i|--id) identification="$2" echo "ID: $identification" shift 2;; -v|--verbose) echo "Please use -n/--name or -i/--id to pass name or identification number respectively" break;; --) break;; esac done |
Flags are not followed by a colon when they are defined in a bash script because they do not take any argument with them unlike options e.g. the -n
option takes a name with it such as Rahul.
Conclusion
A human operator carrying out recurring tasks manually is very inefficient. So such tasks should be automated. Scripts can help us do that.
In this write-up, we have learned how to pass arguments through the command line to a bash script. Command-line arguments help us write dynamic scripts which perform different functions depending on the input.
Moreover, we also discussed different types of arguments. We also touched a bit on predefined variables.
1 Comment
How many numbers of Argument we can pass in command line ?