How to Get Argument in Bash? [4 Methods]

An argument is the parameter or data passed to a script or command when it is executed in the terminal. These arguments can be accessible using positional parameters like $1 or $2 to access the respective 1st and 2nd arguments. Additionally, Bash caters to different special variables such as $@ or $* to get all the arguments supplied by the users. Leveraging the “getopts” command also provides an efficient way to handle options and arguments seamlessly.

In this article, I will show you 4 methods to get an argument in Bash. Moreover, you will learn how to access arguments from the end position. Stay connected!

1. Using Positional Parameter

A positional parameter is a variable that passes a value to a script or function from the command line. These parameters can be used to get either a single argument or multiple arguments in Bash. Check out the following cases to know in detail:

1.1 Get a Single Argument

To get a single argument in Bash script, use a single positional parameter like $1, $2, or $N to pass the 1st, 2nd, or Nth argument from the command line to the script or function. Here’s how:

#!/bin/bash

echo "Linux Distro: $2"
EXPLANATION

The script echoes “Linux Distro” with 2nd argument with the echo command. It uses the $2 positional parameter to get the 2nd argument from the command line.

getting a single argument using positional parameter

As you can see, from the 3 command-line arguments “Ubuntu”, “Fedora”, and “Debian”, the script displayed the 2nd argument “Fedora” using the $2 parameter.

1.2 Get Multiple Arguments

Consider using multiple positional parameters to get more than one argument in the script. It is useful when the user wants to pass multiple arguments to the script. However, this procedure can be laborious to obtain a large number of arguments. In that case, employ the additional methods covered below. Now, follow the Bash script to accomplish this task:

#!/bin/bash

echo "Linux Distro: $1,$2,$3"
EXPLANATION

Here, the echo "Linux Distro: $1,$2,$3" prints a message “Linux Distro” with 1st, 2nd, and 3rd arguments using $1, $2, and $3 positional parameters.

getting multiple arguments using positional parameter

Upon executing the script with ./multiple.sh Ubuntu Fedora Debian, it displayed 3 arguments using the positional parameters ($1, $2, $3).

2. Using “$@”

Integrating the $@ in the script comes in handy to get all the command line arguments passed to the script. The $@ is a special variable holding all the arguments assigned by the users in the terminal. Here’s how to get arguments using $@:

#!/bin/bash

echo "Linux Distro: $@"
EXPLANATION

The message “Linux Distro” is printed on the console with all the command-line arguments using $@ and echo command.

getting all arguments using $@

The picture shows that all the command-line arguments are assigned to the script.

3. Using “$*”

Like $@, $* is another variable that encapsulates all the arguments passed by the users to the Bash script. But $* treats all the arguments as a single string whereas $@ represents each argument as a separate entity. Now copy the script from below to get all the arguments using $*:

#!/bin/bash

echo "Linux Distro: $*"
EXPLANATION

In this script, echo "Linux Distro: $*" displays all the arguments passed to the script using $*.

getting all arguments using special variable

All the command-line arguments are assigned to the script as shown in the image.

4. Using “getopts” Command

Using getopts, the arguments from the command line can be passed directly to the Bash variables. This command parses the command-line options and arguments that allow users to handle arguments in a structured way. So to get multiple arguments, incorporate the case statement with the getopts command. Here’s how:

#!/bin/bash
while getopts ":a:b:c:" opt; do

 case $opt in
  a)
   echo "Option a has been provided with argument: $OPTARG"
   ;;
  b)
   echo "Option b has been provided with argument: $OPTARG"
   ;;
  c)
   echo "Option c has been provided with argument: $OPTARG"
   ;;
 esac
done
EXPLANATION

Here, while getopts ":a:b:c:" opt; do starts a while loop and sets the options a, b, and c to take arguments. Inside the loop, case $opt in begins the case statement and checks which option is being processed. Options a, b, and c prints their corresponding arguments stored in the $OPTARG variable. Finally, the loop ends when the processing of all options is done.

getting arguments using getopts

You can see that the given arguments are passed to the corresponding options.

How to Get Bash Script Argument From End Position?

To get an argument from the end position in Bash, use ${@: -1} that employs parameter expansion to access the last argument passed to the script from the list. Here is how it works:

#!/bin/bash

echo "Last argument is:${@: -1}"
EXPLANATION

This script prints the last argument from the argument list ($@) using the echo command. In this syntax, the -1 with parameter expansion extracts the last argument.

getting last argument using parameter expansion

Upon executing the script with ./last-arg.sh 1 2 3 4, it shows 4 (last argument).

Note: To get the Nth arguments from the list, you can use ${@: -N}.

Alternatively, you can use a for loop in conjunction with indirect expansion (a way of using a variable value as the name of another variable) to get the argument from the last position. Check out the bash script to do so:

#!/bin/bash

last_arg=""
for (( n= $#; n > 0; n-- )); do
 last_arg="${!n}"  # Indirect expansion to access argument by position
 break;
done
echo "Last argument is: $last_arg"
EXPLANATION

In this script, the last_arg is assigned to an empty string. Then the for loop iterates over the total number of arguments ($#) passed to the script as long as the variable n is greater than 0. After that, it uses indirect expansion ${!n} that retrieves the argument at position n and assigns it to the “last_arg” variable. After retrieving the last argument, the break command breaks the loop. Finally, the script echoes the last argument in the terminal.

After running the script with ./last-arg2.sh apple banana cherry, the output will be as follows:

getting last argument using for loop

Conclusion

Getting an argument from the command line is essential for writing robust and efficient scripts. In this article, I have provided 4 ways to get an argument from bash. You can choose any method according to your preference. Also, you can use parameter expansion to get the last argument passed to the script. Good luck!

People Also Ask

How do I get an argument in bash?

To get an argument in Bash, use positional parameters $1, $2,…$N where N=1,2,3,4,……,N. These parameters represent the 1st, 2nd, and Nth argument passed to the script respectively. For instance, to access the 2nd argument “banana” from the list “apple banana cherry”, use echo $2 in the Bash script.

How to take arguments from file in bash?

To take arguments from a file in Bash, use the input redirection operator < to redirect the file contents to the standard input of the Bash script. For example, to take arguments from the “arg.txt” file, use a while loop with the “read” command to read each line of the file contents and take them as arguments. Here is the bash script for this:

#!/bin/bash
while read argument; do
 echo "Arguments: $argument"
done

Then direct the file contents to the Bash script “my_script.sh” using ./my_script.sh < arg.txt. This will display each line of the file as an argument.

How do I get the first argument in bash?

To get the first argument in Bash, use $1 as it represents the 1st argument passed to the script.

How do I get multiple arguments in Bash?

To get multiple arguments in Bash, use $1 $2,….,$N that represents the 1st, 2nd, and Nth argument passed to the script. For example, to get 3 arguments like apple, banana, and cherry from the command line, write echo $1 $2 $3 in the Bash script.

What is argument in Linux command?

An argument is the additional information provided to the script or command when it is executed in the shell. It can be used to modify the behavior of a command. For instance, the command ls /home will show the list of files and directories in the ‘home’ directory. In this case, /home is an argument passed to the command that changes the behavior of ‘ls’.

Can bash functions accept arguments?

Yes, Bash functions accept arguments. You can use positional parameters like $1, $2, or $3 to access the 1st, 2nd, and 3rd arguments passed to the function respectively.

How to Use Command Line Arguments in a Bash Script?

To use command line arguments in a Bash script, write $@ in the script that represents all the arguments passed to the script. However, to use any particular argument, you can use positional parameters. If you want to access the Nth argument, type $N in the Bash script.

Related Articles


<< Go Back to Argument in Bash Script | Bash Functions | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment