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.
Key Takeaways
- Learning about Bash Input Argument.
- Knowing how to use Bash Input Argument.
Free Downloads
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.
Example 1: Sum up Two Numbers in Bash
In this example, I will show you how to sum two numbers where I will take input from the user using input arguments.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano sum.sh
❸ Copy the script mentioned below:
#!/bin/bash
number1=$1
number2=$2
sum=$((number1 + number2))
echo "The sum of $number1 and $number2 is: $sum"
Here #!/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
In this example, I will take user information using the Bash input argument and print them on the terminal. Script (user_info.sh) > 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: Note: The double quotation denotes Walid Al Asad as a single entity. Here I will pass a filename as an argument and check if the file exits or not. Script (check_file.sh) > 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: 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. Script (flags.sh) > The script starts with #!/bin/bash that specifies the interpreter. 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. The 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: 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.
Related Articles << Go Back to Bash Input | Bash I/O | Bash Scripting Tutorial❻ 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.
Example 2: Take User Information Using Bash Input Argument
#!/bin/bash
echo "Name: $1"
echo "Age: $2"
echo "City: $3"
echo “Country: $4”
echo "Profession: $5"
./user_info.sh “Walid Al Asad” 26 Dhaka Bangladesh Blogger
In the output, the script is showing user information which was given as arguments.
Example 3: Check If a File Exists Using Bash Script
#!/bin/bash
filename=$1
if [[ -e "$filename" ]]
then
echo "The file $filename exists."
else
echo "The file $filename does not exist."
fi
./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.
Example 4: Take Argument with Flags Using Bash Script
#!/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
./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
People Also Ask