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 hardcoded values. Like any other programming or scripting language, bash scripting also supports command-line arguments.

In this article, we will explore the syntax for command-line arguments, examine practical examples, and discuss the special variables used to handle these arguments effectively.

Command-line Argument Syntax

We already know how to run a bash script in Linux using either bash or sh command followed by the script filename. To pass command-line arguments, we simply add them after the script name, separated by spaces.

bash <script_name> <arg1> <arg2> ... <argN>

Here is a practical example

bash add.sh 1 2 3

Example 1 Sum of Three Integers

Let's create a script that accepts three integers as command-line arguments and calculates their sum.

#!/bin/bash

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

sum=$(($1+$2+$3))
echo "Sum = $sum"

Run the script with

$ bash sum.sh 1 2 12
First argument is--> 1
Second argument is--> 2
Third argument is--> 12
Sum = 15

Key Points

  • $1 stores the first argument

  • $2 stores the second argument

  • $3 stores the third argument

  • Arguments are referenced by their position number

Special Variables for Command-Line Arguments

Bash provides several special variables to work with command-line arguments

Variable Description
$1...$n Positional arguments (first, second, etc.)
$0 Script filename
$# Total number of arguments passed
$@ All arguments as separate quoted strings
$* All arguments as a single string
$$ Process ID of current shell
$? Exit status of last command
$! Process ID of last background command

Example 2 Demonstrating Special Variables

#!/bin/bash

echo "1. 1st argument is --> $1"
echo "2. 2nd argument is --> $2"
echo "3. File name is --> $0"
echo "4. Total number of arguments --> $#"

echo "5. All arguments using \$@:"
i=1
for arg in "$@"
do
   echo "   argument-$i is: $arg"
   i=$((i + 1))
done

echo "6. Process ID of current shell --> $$"

echo "7. All arguments using \$*:"
echo "   $*"

echo "8. Executing ls command"
ls > /dev/null

echo "9. Exit status of last command --> $?"

Run with

$ bash special-var.sh hello "world test" 123
1. 1st argument is --> hello
2. 2nd argument is --> world test
3. File name is --> special-var.sh
4. Total number of arguments --> 3
5. All arguments using $@:
   argument-1 is: hello
   argument-2 is: world test
   argument-3 is: 123
6. Process ID of current shell --> 3061
7. All arguments using $*:
   hello world test 123
8. Executing ls command
9. Exit status of last command --> 0

Example 3 Using Flags with getopts

Flags allow you to pass arguments with identifiers, making scripts more flexible and user-friendly. The getopts command helps parse these flags.

#!/bin/bash

while getopts c:s:d: flag
do
   case "${flag}" in
      d) district=${OPTARG};;
      s) state=${OPTARG};;
      c) country=${OPTARG};;
   esac
done

echo "District: $district"
echo "State: $state"
echo "Country: $country"

Run with flags in any order

$ bash flag.sh -c INDIA -d BANGALORE -s KARNATAKA
District: BANGALORE
State: KARNATAKA
Country: INDIA

Advantages of Using Flags

  • Order independence Arguments can be passed in any sequence

  • Self-documenting Flags make the purpose of each argument clear

  • Optional parameters Some flags can be optional with default values

  • Better error handling Easier to validate specific parameters

Conclusion

Command-line arguments make bash scripts more dynamic and reusable by allowing external input. Special variables like $1, $#, and $@ provide powerful ways to handle these arguments, while getopts enables sophisticated flag-based parameter parsing for professional script interfaces.

Updated on: 2026-03-17T09:01:38+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements