How to Pass Command Line Arguments to Bash Script


For Linux system administrators and developers, the ability to write Bash scripts is important. It enables us to automate routine processes and carry out difficult ones with little effort. One of the most crucial aspects of bash scripting is the ability to give information to a script as it is being executed. The ability to alter a script's behaviour without changing its code is made possible through command line parameters.

In this article the usage of command line parameters in a bash script will be discussed. We will discuss some methods for handling command line arguments and give illustrations for each method. We will be able to use command line arguments in bash scripts with confidence after reading this article.

Using Positional Parameters in Command Line Arguments

The easiest approach to use command line arguments in a bash script is using positional parameters. The variable $1 holds the first argument, the variable $2 holds the second argument, and so on.

The following script is using positional parameters −

echo "Name, $1"
echo “Age, $2”

Let us see the output.

$ ./ Somdeb 22

The script will return the following result −

Name, Somdeb
“Age, 22”

"Getopts" to Parse Command Line Arguments

Getopts is a built-in shell command that allows us to parse command line options and arguments in a flexible and efficient way. Getopts uses a while loop to iterate through each command line argument and parse its options.

Here's an example script that uses getopts −

while getopts ":a:b:" opt; do
   case ${opt} in
      a )
         echo "Option -a stores the value: $OPTARG"
      b )
         echo "Option -b htores the value: $OPTARG"
      \? )
         echo "Wrong option: -$OPTARG" 1>&2
      : )
         echo "Option -$OPTARG requires an argument." 1>&2

This script converts the -a and -b command line parameters using the getopts command. The colon (:) that follows each option letter indicates that it requires an argument. The OPTARG variable holds the value of the options argument, while the opt variable holds the current option letter.

$ ./ -a Golf -b Football

The script will return the following result −

Option -a stores the value: Golf
Option -b htores the value: Football

Using “loops” in the Command Line Arguments

Loops are used to repeatedly iterate over the command-line inputs and take a particular action for each one. When we need to analyse each argument separately or take a specific action in response to each argument, this method is helpful.

Here is an example of a script that loops over the command-line options using a "for" loop −

for arg in "$@"; do
   echo "Processing argument: $arg"
   # perform some action with the argument here

The command line arguments are all accessed via the $@ variable in this script, and each argument is iterated over using the for loop. Before utilising an argument, we emit a message indicating that it is being processed.

Let us run the script −

$ ./ Hello, Welcome to India.

The script will return the following result −

Processing argument: Hello,
Processing argument: Welcome
Processing argument: to
Processing argument: India.

Here, the script simply returns a message for the each input provided. However, we could easily change the script to take a different action for each argument according to our particular needs.

"flags" to parse Command Line Arguments

Using “flags” is an another way to use command line arguments.

while getopts "ab" opt; do
   case $opt in
      echo "Invalid option: -$OPTARG" >&2
      exit 1
if $flag_a; then
   echo "Option -a has been set"
if $flag_b; then
   echo "Option -b has been set"

This example defines two flags that are, "flag_a" and "flag_b," both of which are set to "false."

$ ./ -a -b
Option -a has been set
Option -b has been set

Flags are helpful when we don't want to supply an argument value but yet want to activate or disable a specific behaviour or feature of the script. Additionally, they improve the user-friendliness of the command line interface because users don't have to remember the proper sequence of positional arguments and can turn the options they want on or off.


Finally, we discussed how to use command line arguments in bash scripts using positional parameters, getopts, shift, flag and loops. By comprehending these tactics, we may develop scripts that are robust and flexible and automate a variety of processes. By using the knowledge and examples in this article, we may become proficient in bash scripting.

Updated on: 14-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started