In Bash Scripting, input arguments refer to the values that are passed to a Bash script or function when it is executed. For instance, let’s say you are writing a script that sums up two numbers and shows the output. Here, you need to pass those two numbers to the script and you can use the Bash input argument for that purpose. Bash Input Argument can be accessed using $1, $2, $3 etc where $1 is the first argument, $2 second and so forth.
4 Practical Examples of Input Argument in Bash
In this section, I will provide some examples of Bash input argument. To grasp the concept, do the examples by hand.
1. Sum up Two Numbers in Bash Using Input Argument
In this example, I will show you how to sum two numbers where I will take input from the user using Bash input argument:
- At first, launch an Ubuntu Terminal.
- Write the following command to open a file in Nano:
nano sum.sh
EXPLANATION- nano: Opens a file in the Nano text editor.
- sh: Name of the file.
- Copy the script mentioned below:
#!/bin/bash number1=$1 number2=$2 sum=$((number1 + number2)) echo "The sum of $number1 and $number2 is: $sum"
EXPLANATIONHere #!/bin/bash is called Shebang that specifies the interpreter. In
number1=$1
, $1 denotes the first argument and the first argument ($1) is stored in the variable number1. Similarly,$2
denotes second argument and is stored in the variable number2.Now in
sum=$((number1 + number2))
, $(()) is arithmetic expansion, which performs mathematical calculations within double parentheses. In this line, the summation of number1 and number2 is stored in the variable sum. Finally, in echo “The sum of $number1 and $number2 is: $sum”, the echo command is used to print the output. Here the$
sign before variables ($number1, $number2, $sum), substitutes the variables with actual values. - Press CTRL+O and ENTER to save the file; CTRL+X to exit.
- Use the following command to make the file executable:
chmod u+x sum.sh
EXPLANATION- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- sh: Name of the script.
- Run the script by the following command:
./sum.sh 3 7
In the output, you can see the sum of 3 and 7 is 10.Note: Here 3 and 7 are input arguments.
2. Take User Information Using Bash Input Argument
In this example, I will take user information using the Bash input argument and print them on the terminal:
#!/bin/bash
echo "Name: $1"
echo "Age: $2"
echo "City: $3"
echo "Country: $4"
echo "Profession: $5"
In this script, the inputs are stored in special variables $1, $2, $3, $4, $5. Then the inputs are printed on the terminal using the echo
command.
Now, run the script by the following command:
./user_info.sh "Walid Al Asad" 26 Dhaka Bangladesh Blogger
Note: The double quotation denotes Walid Al Asad as a single entity.
3. Check If a File Exists Using Bash Input Argument
Here I will pass a filename as an input argument and check if the file exits or not:
#!/bin/bash
filename=$1
if [[ -e "$filename" ]]
then
echo "The file $filename exists."
else
echo "The file $filename does not exist."
fi
The filename=$1
line stores the argument in the filename variable. In if else statement, -e
checks if a file or directory exists. If the condition is true, it shows the file exists, otherwise shows the file doesn’t exits. In addition, here $filename is a way to access the filename variable. It is enclosed in double quotation (“”) to handle spaces and special characters.
Now run the script by the following command:
./check_file.sh hello.txt
As there is no “hello.txt” file in my directory, the output is showing that the file doesn’t exist.
4. Take Input Argument With Flags in Bash
Here, I will show you how to take arguments with flags. What is a flag? A flag modifies the behavior of a command or script. I will use getopts
command here which provides a way to handle flags. My script will take a filename and a number as output and print them on the terminal. Moreover, it will throw an error if an option is invalid:
#!/bin/bash
while getopts "f:n:" opt; do
case $opt in
f)
filename=$OPTARG
echo "File: $filename"
;;
n)
num=$OPTARG
echo "Number: $num"
;;
\?)
echo "Invalid option: -$OPTARG"
;;
esac
done
The script starts with #!/bin/bash that specifies the interpreter. The while getopts "f:n:" opt; do
starts a loop that uses the getopts
command to take options and arguments. Here are two options- “f:” and “n:”. The script expects two options: -f
followed by a filename, and -n
followed by a number. Moreover, the colon (“:”) indicates that the option requires an argument.f)
section assigns the argument ($OPTARG) of option “-f” to the variable filename. Then echoes the filename. Furthermore, the n)
section assigns the argument ($OPTARG) of option “-n” to the variable num. Then echoes the number. In addition, the \?)
pattern is used to handle the situation when a provided option is not recognized by the script. And then a line is printed indicating an invalid option was provided. Finally, esac
and done
end the “case” statement and “whole” statement respectively.
Execute the script by the following command:
./flags.sh -f myfile.txt -n 10
Here, the argument of flag “-f” is myfile.txt and “-n” is 10. And you can see myfile.txt and 10 in the output.
Conclusion
In this article, I have tried my best to explain the Bash input argument. Practice the examples here. Hopefully, it will help you with your journey of Bash scripting.
People Also Ask
What is argument in shell script?
An argument in shell script is a value that is passed to the script when it is executed. Arguments can be useful for making the script dynamic and responsive.
How to take input arguments in shell script?
To take input arguments in a shell script, you can use the special variables $1, $2, $3. By using those variables, you can access the first, second and third argument respectively.
What is $1 and $2 in shell script?
$1
and $2
in shell script are positional parameters that hold the values passed to the script on the command line.
What is $0 in bash?
$0
in bash is a special parameter that holds the name of the script. For instance, if your script name is myscript.sh, then $0 will be myscript.sh.
Related Articles
- How to Use Bash Input Parameter? [5 Practical Cases]
- How to Read User Input in Bash [5 Practical Cases]
- How to Wait for User Input in Bash [With 4 Practical Examples]
- How to Read CSV Files in Bash? [2 Methods]
<< Go Back to Bash Input | Bash I/O | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners