How to Use Bash Command Line Arguments in Scripts

bash script_name.sh arg1 arg2 .. argN

In this article we’ll learn the most important and common ways to pass command-line arguments to our bash script.

What are Command Line Arguments

In bash scripting we use command line arguments as inputs. These input options are specific with the bash script on terminal during the run time.

There are three types of parameters:

  • positional parameters
  • special parameters
  • and variables.

Each argument passed to a script is stored in the exact same order in corresponding with bash variables when executing the .sh file (including the bash script name).

The index of the command line argument starts from 0. This indicates that the name of the script itself is stored in $0, which receives the variable values from command line arguments.

So the index of the first argument passed to a script is $1; the second argument value is stored in $2, until we reach the n’th argument (Access parameters by position).

$ bash myscript.sh Value1 Value2 Value3 Value4 Value5 Value6 Value7
$0 $1 $2 $3 $4 $5 $6 $7

In the next paragraphs we are going to dive into how to master different ways to process the arguments passed to a .sh script, and how to structure them.

What are Positional Parameters

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. In other words, when a bash script is invoked from the command line, it uses a tool called positional parameters to allow data to be entered into it, following the same order as declared in the program.

We are going to start with step by step examples to fully understand the topic at hand. Let’s create a simple bash script called my-zero-argument-script.sh that returns the value stored at $0.

vi my-zero-argument-script.sh

The content of our simple script:

vi my-zero-argument-script.sh
echo "My filename is : $0 ";

Let’s run the script passing no command line arguments:

bash my-zero-argument-script.sh

As mentioned before, the output from this script is our file name: “My filename is: my-zero-argument-script.sh”, since the name of the script is stored in $0.

Output
My filename is: my-zero-argument-script.sh

Now we are going to pass positional arguments to our bash script. But we’ll have to create new content to make it possible. Open a new file using your favorite text editor:

vi my-positional-arguments-script.sh

The content of our second script:

my-positional-arguments-script.sh
echo "First arg: $1";
echo "Second arg: $2";
echo "Third arg: $3";

Choose your values and execute the script:

bash my-positional-arguments-script.sh what is Linux?

The output will be:

Output
First arg: what
Second arg: is
Third arg: Linux?

If we choose to pass arguments that contain multiple words, we achieve that with quotes. Let’s run our script using new parameters:

bash my-positional-arguments-script.sh "Is Linux" "An Operating System" "or a Kernel?"

The output of the above command line:

Output
First arg: Is Linux
Second arg: An Operating System
Third arg: or a Kernel?

Command Line Arguments with getopts

The builtin getopts function is used to parse command-line arguments. It allows us to pass arguments to a script, in the form of single characters preceded by a hyphen.

This method comes in handy when we want to generate a custom report or store data that are based on command line arguments values.

Create a bash file named my-getopts-script.sh with the following script:

my-getopts-script.sh
while getopts ":n:r:a:" arg; do
    case $arg in
        n) myname=$OPTARG;;
        c) myride=$OPTARG;;
        a) myaddress=$OPTARG;;
    esac
done
echo "My Name: $myname "
echo "My ride: $myride "
echo "My Street Address: $myaddress "n

Our bash file takes 3 options (-n –r -a). The case statement is used to store each argument value in the corresponding variable. $OPTARG keyword represents the parameter value.

Choose the arguments and run the script:

bash my-getopts-script.sh –n Zack –r rocket –a "Blue ciel Avenue Z"

The output of our command line:

Output
My Name: Zack
My ride: Rocket
My Street Address: Blue ciel Avenue Z

Using getopts formula is more suited when passing data in a custom format to other programs or databases.

Using ‘[email protected]’ special parameter

Imagine that we want to handle an unspecified number of arguments passed to our script!

As you already guessed the first two methods aren’t the only methods available to handle long sets of command line arguments. Using “[email protected]” will return an array of all argument values, then we will loop over them.

This method is a simple way to read all our command line argument values.

Let’s try understanding [email protected] by using our previous example. Create a bash file named my-special-parameter-script.sh with the following script:

vi my-special-parameter-script.sh
my-special-parameter-script.sh
args_array="[email protected]"
echo "My args array: $args_array"
echo $#

Note that $# returns the total number of arguments.

Run the script:

bash my-special-parameter-script.sh Zack rocket "Blue ciel Avenue Z"

The output will be:

Output
My args array: Zack rocket Blue ciel Avenue Z
3

After we understand the role of [email protected], we are going to handle passing and printing an unknown set of command line argument values. Let’s create a bash file named my-looping-args-script.sh with the following script:

my-looping-args-script.sh
argsv="[email protected]"
for arg in "[email protected]" ; do
    echo "$arg"
done

Run the script:

bash my-looping-args-script.sh OWASP API "Who Am I"

The output will be:

Output
OWASP
API
Who Am I

Using ‘$#’ special parameter

This method is somewhat similar to the previous example.

Instead of storing all command line argument values into an array, $# parameter will return the total number of arguments passed to script.

Then we’ll be using the shift 1 command inside our loop to shift the index 1 from the first argument to the next ($2 will hold the first index $1… and so on) 1 step, until we process the total of our command line argument values.

First we are going to use our previous example to understand $#. Create a bash file named my-special-parameter2-script.sh with the following script:

my-special-parameter2-script.sh
echo "First arg: $1"
echo "Second arg: $2"
echo "Third arg: $3"
echo "The total number of arguments: $#"

Run the script:

bash my-special-parameter2-script.sh OWASP API "Who Am I"

The output will be:

Output
First arg: OWASP
Second arg: API
Third arg: Who Am I
The total number of arguments: 3

After we clarified the role of this special parameter, we’ll put it to good use in processing and printing all command line arguments passed to a bash script. Create a bash file named my-looping-args2-script.sh with the following script:

my-looping-args2-script.sh
while [ $# -gt 0 ]
do
    echo $1
    shift 1
done

Run the script:

bash my-looping-args2-script.sh "All of the common" "types in" TypeScript

The output will be:

Output
All of the common
types in
TypeScript

Conclusion

In this article we learned different ways to pass command line arguments to a bash script. We used positional parameters, and getopts formula to enter data into the bash script. We also learned about special parameters ($# - [email protected]) that return the total number of arguments passed to a script and the values of all arguments; these parameters helped us to process unknown argument numbers passed to a bash script.

We hope you’ve learned how to use command line arguments. If you have any feedback or questions feel free to let us know in the comments.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Source Command
Read More

Bash Source Command

The Bash source command reads and executes commands from a specific file as an argument within the current…