How to Use Command Line Arguments in a Bash Script


In general, there are many ways to pass input to programming or scripting languages and sometimes there is a need to pass input from the command line instead of input from the code. Like any other programming or scripting language bash scripting also support the command-line argument.

In this article, we will try to explore the syntax for command-line arguments and then we will see some examples for command-line arguments and discuss the special variables.

Command-line Argument Syntax

We already know how to run a bash script in Linux. We can use either bash or sh command and then the bash file name.

Now, to pass the command-line argument we can pass arguments separated by space.

bash <bash script file name> <command line argument 1> <command line argument 2> … <command line argument n>

Here is an actual example

bash 1 2 3

Next, we are going to try some command-line argument bash scripting.

Approach 1: Pass three integers through the command line and get the sum



echo "First argument is--> $1"
echo "Second argument is--> $2"
echo "Third argument is--> $3"

echo "Sum = $sum"

Then run the bash script like

$ bash 1 2 12


First argument is--> 1
Second argument is--> 2
Third argument is--> 12
Sum = 15


From the above scripting, we have got below points

  • First argument is stored in $1

  • Second argument is stored in $2

  • Third argument is stored in $3

Approach 2: List of special variables used in bash scripting

  • $1…$n − This is a positional argument. We already know this from the previous example.

  • $0 − This indicates the scripting file name. Example:

  • $# − This holds the total number of arguments passed.

  • $@ − This holds the values of each argument.

  • $$ − This holds the PID of the current shell.

  • $* − Using this we can get all the arguments in a single parameter.

  • $? − This holds the exit status id of the recent command.

  • $! − This holds the PID of the recent command.

Let us use all these in a script and see the output.



echo "1. 1st argument is --> $1"
echo -e "1. 2nd argument is --> $2"

echo "2. File name is --> $0"

echo "3. Total number of arguments passed --> $#"

echo "4."
for arg in "$@"
   echo "argument-$i is : $arg";
   i=$((i + 1));

echo "5. PID of current shell --> $$"

echo "6."
for arg in "$*"
   echo "argument-$i is: $arg";
   i=$((i + 1));

echo "Executing ls command"

echo "7. Exit status of last command --> $?"
echo "Executing firefox command"
firefox &

echo "8. PID of last command --> $!"
killall firefox


$ bash 1 "2 4" 5

1. 1st argument is --> 1
1. 2nd argument is --> 2 4
2. File name is -->
3. Total number of arguments passed --> 3
argument-1 is : 1
argument-2 is : 2 4
argument-3 is : 5
5. PID of current shell --> 3061
argument-1 is: 1 2 4 5
Executing ls command
7. Exit status of last command --> 0
Executing firefox command
8. PID of last command --> 3063

Approach 3: Use of Flags

There is a way to pass a command-line argument along with a flag. Flag is a single letter starting with a hyphen before the argument.

Let us see the code and then it will be easy to understand the concept.


while getopts c:s:d: flag
   case "${flag}" in
      d) district=${OPTARG};;
      s) state=${OPTARG};;
      c) country=${OPTARG};;
echo "District: $district";
echo "State: $state";
echo "Country: $country";



Country: INDIA

From the above code, we can see the advantages of using the flag in the command line. Now we know that we do not have to pass the command line arguments in a sequence. Instead, we can use flag.


In this article, we have learned how to use command-line arguments in bash script in Linux. In addition, various special variables help to parse the command line arguments. So, using the command line argument we can do better and more efficient bash scripting.