FUNDAMENTALS A Complete Guide for Beginners
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:
- Using Quotation:
script.sh 'argument'
orscript.sh "argument"
- Using Backslash:
script.sh 1\*2
- Using Flags:
script.sh -flag argument
- Using “xargs” Command:
xargs -n1 script.sh < argument
- 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: $@"
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:
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"
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"
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
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
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
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
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”.
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 !$
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
- How to Check Number of Arguments in Bash? [3 Methods]
- How to Use First Argument in Bash Script [5 Cases]
- How to Get Argument in Bash? [4 Methods]
- How to Parse Optional Arguments in Bash Script [2 Ways]
- How to Pass Array as an Argument in Bash Function? [3 Ways]
- How to Set Default Argument in Bash [2 Methods]
- How to Use OPTARG in Bash [3 Practical Examples]
- How to Use “getopts” in Bash [Complete Guide]
- [Solved] “bash: Argument list too long” Error
<< Go Back to Argument in Bash Script | Bash Functions | Bash Scripting Tutorial