How to Pass Arguments to Bash Script? [5 Methods]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To pass arguments to a Bash script, just write the arguments in the command line after the script name (separated by spaces). For example, let’s say you have a script named “script.sh” that takes an argument:

script.sh hello

Here, the “hello” is passed to the “script.sh” as an argument. Some other methods to pass arguments to the Bash script are:

  1. Using Quotation: script.sh 'argument' or script.sh "argument"
  2. Using Backslash: script.sh 1\*2
  3. Using Flags: script.sh -flag argument
  4. Using “xargs” Command: xargs -n1 script.sh < argument
  5. Using !$: script.sh !$

Let’s see these methods in detail in the following article.

1. Using Quotation

You can pass arguments in single or double quotation marks. A single quotation mark passes an argument that has spaces between it. Moreover, an argument that needs to be evaluated should be passed using double quotes. See the process practically where I will pass arguments using both single & double quotes to the following script named “my_script.sh”:

#!/bin/bash

echo "Argument: $@"
EXPLANATION

The script uses $@ which contains all the arguments passed to the script. Then it prints the argument in the terminal using the echo command.

1.1 Single Quote

When there is space between the characters in an argument, it should be passed using a single quotation. On the other hand, Bash treats it as if it is a multiple argument. For example, if you want to pass “hello world” as one argument, you should include it in the single quotation ‘hello world’. Otherwise, Bash will pass “hello” and “world” as separate arguments. Here’s the command to pass the argument “hello world” to the “my_script.sh”:

./my_script.sh 'hello world'

After running this command, you will get the output as follows:

passing argument using single quote

1.2 Double Quotes

Using double quotes is mandatory when users want to pass arguments that require evaluation like passing environment variables. In this example, I will pass the $USER environment variable that contains the current username logged into the system to the Bash script “my_script.sh”:

./my_script.sh "$USER"

passing arguments using double quotes

Upon executing the script, the value of the USER variable which is the current username is passed to the Bash script.

2. Using Backslash

To pass arguments including special characters ($, *, &) from the command line to the bash script, it is safe to integrate backslash within arguments to escape those characters. Here’s how:

#!/bin/bash

echo "Argument: $1 $2"
EXPLANATION

Here, $1 and $2 store the 1st and 2nd arguments passed to the script. After that, the echo command prints the arguments on the console.

Now, execute the script with arguments by writing this command:

./script1.sh hello\$ 3\*5

passing arguments using backslash

The picture shows that all the command-line arguments containing special characters are assigned to the Bash script.

3. Using Flags

To pass arguments to the Bash script, incorporate flags with the getopts command which handles the command line arguments and flags structurally. This command passes the arguments directly to the Bash variables through flags. Copy the script from below to achieve this task:

#!/bin/bash

while getopts ":a:b:" opt; do
 case $opt in
  a)
   echo "Argument of option a: $OPTARG"
   ;;
  b)
   echo "Argument of option b: $OPTARG"
   ;;
 esac
done
EXPLANATION

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

Now run the following command to pass the arguments:

./script2.sh -a hello -b 12

passing arguments using flags

Arguments after flags a (hello) and b (12) are assigned to the script.

4. Using “xargs” Command

The xargs command in conjunction with the pipe operator can pass arguments to the Bash scripts. This xargs command reads items from the standard input separated by blanks and executes a specified command using the items as arguments. Here is how this command passes arguments:

#!/bin/bash

echo "Arguments received: $@"

After writing this script in the “script3.sh”, execute this command to pass the arguments:

xargs -n1 ./script3.sh < hhh.txt
EXPLANATION

The < hhh.txt part redirects the contents of the file “hhh.txt” to the standard input of xargs command. Here, the xargs command with the -n1 option treats each line of the file contents (separated by spaces) as a separate argument and passes them to the “script3.sh”.

passing argument using xargs

All the contents, separated by blanks are passed to the script as separate arguments.

5. Using !$

The operator !$ evaluates the last argument from the previous command. So this parameter can be used to pass arguments to another command or Bash script. Here’s the Bash script to pass arguments using !$:

#!/bin/bash

echo "Argument received:$1"

Now, run the following command to change the current directory to the user’s home directory:

cd $HOME

Afterward, execute the script with $! that will pass the last cd command’s argument $HOME to the “script4.sh”:

./script4.sh !$

passing arguments using special operator

The value of the $HOME argument /home/mou has been passed to the script.

Some Tips to Pass Arguments to Bash Script

  • Provide Descriptive Argument Names: To make your script readable, always provide meaningful names for the arguments.
  • Use Comments: Give clear instructions to pass arguments using comments in the scripts.
  • Validate Argument Types: Check argument types whether it is number or filename to prevent unexpected script behavior.
  • Use “getopts” Command for Complex Options: To handle different arguments and options, consider using getopts command.
  • Employ “shift” Command: Use shift command for Iterating through the argument. This command removes the processed argument from the positional parameter list.

Conclusion

To conclude, I have explained 5 methods to pass arguments to the Bash script. Additionally, I have added some tips to follow that will make your script more robust, user-friendly, and error-free. Happy Scripting!

People Also Ask

How do I pass an argument in Bash?

To pass an argument in Bash, simply write the argument after the script name while executing the script. For example, to pass “arg1” argument to a script named “script.sh”, write script.sh arg1 in the command line.

How to Access Arguments in Bash?

To access arguments in Bash, use positional parameters $1, $2,…$N. For instance, to access the 2nd argument “banana” from the list “apple banana cherry”, use echo $2 in the Bash script.

How to pass arguments to Bash function?

To pass arguments to the Bash function, write the arguments after the function name when it is called. Let’s say, you want to pass arguments “arg1” and ‘arg2” to a Bash function named my_function, you can use this syntax: my_function ar1 arg2

How to pass 2 arguments in shell script?

To pass 2 arguments in the shell script, write the arguments after the script name while executing. For instance, to pass “arg1” and “arg2” to a script.sh, write this code: script.sh arg1 arg2

What is the importance of arguments in scripting and automation?

Arguments are used to change the behavior of Bash scripts. When you pass arguments to a Bash script, you can control different aspects of the execution of the script, such as the input data, the output destinations, the processing options, etc. This makes the script highly customizable and flexible. By using arguments, you can create scripts and automate tools that work well on different data sets or perform different operations with little manual input, saving time and increasing productivity.

What does $@ mean in Bash?

$@ is a special parameter in bash that represents all the position parameters (arguments) passed to the script or function.

Related Articles


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

Rate this post
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