
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
How to Use Command Line Arguments in a Bash Script
Introduction
In general, there are many ways to pass input to programming or scripting languages and sometimes there is a need to pass input from the command line instead of input from the code. Like any other programming or scripting language bash scripting also support the command-line argument.
In this article, we will try to explore the syntax for command-line arguments and then we will see some examples for command-line arguments and discuss the special variables.
Command-line Argument Syntax
We already know how to run a bash script in Linux. We can use either bash or sh command and then the bash file name.
Now, to pass the command-line argument we can pass arguments separated by space.
bash <bash script file name> <command line argument 1> <command line argument 2> … <command line argument n>
Here is an actual example
bash add.sh 1 2 3
Next, we are going to try some command-line argument bash scripting.
Approach 1: Pass three integers through the command line and get the sum
Example
#!/bin/bash echo "First argument is--> $1" echo "Second argument is--> $2" echo "Third argument is--> $3" sum=$((1+$2+$3)) echo "Sum = $sum"
Then run the bash script like
$ bash sum.sh 1 2 12
Output
First argument is--> 1 Second argument is--> 2 Third argument is--> 12 Sum = 15
Explanation
From the above scripting, we have got below points
First argument is stored in $1
Second argument is stored in $2
Third argument is stored in $3
Approach 2: List of special variables used in bash scripting
$1…$n − This is a positional argument. We already know this from the previous example.
$0 − This indicates the scripting file name. Example: sum.sh
$# − This holds the total number of arguments passed.
$@ − This holds the values of each argument.
$$ − This holds the PID of the current shell.
$* − Using this we can get all the arguments in a single parameter.
$? − This holds the exit status id of the recent command.
$! − This holds the PID of the recent command.
Let us use all these in a script and see the output.
Example
#!/bin/bash echo "1. 1st argument is --> $1" echo -e "1. 2nd argument is --> $2" echo "2. File name is --> $0" echo "3. Total number of arguments passed --> $#" echo "4." i=1; for arg in "$@" do echo "argument-$i is : $arg"; i=$((i + 1)); done echo "5. PID of current shell --> $$" echo "6." i=1; for arg in "$*" do echo "argument-$i is: $arg"; i=$((i + 1)); done echo "Executing ls command" ls echo "7. Exit status of last command --> $?" echo "Executing firefox command" firefox & echo "8. PID of last command --> $!" killall firefox
Output
$ bash special-var.sh 1 "2 4" 5 1. 1st argument is --> 1 1. 2nd argument is --> 2 4 2. File name is --> special-var.sh 3. Total number of arguments passed --> 3 4. argument-1 is : 1 argument-2 is : 2 4 argument-3 is : 5 5. PID of current shell --> 3061 6. argument-1 is: 1 2 4 5 Executing ls command special-var.sh sum.sh 7. Exit status of last command --> 0 Executing firefox command 8. PID of last command --> 3063
Approach 3: Use of Flags
There is a way to pass a command-line argument along with a flag. Flag is a single letter starting with a hyphen before the argument.
Let us see the code and then it will be easy to understand the concept.
Example
#!/bin/bash while getopts c:s:d: flag do case "${flag}" in d) district=${OPTARG};; s) state=${OPTARG};; c) country=${OPTARG};; esac done echo "District: $district"; echo "State: $state"; echo "Country: $country";
Output
$ bash flag.sh -c INDIA -d BANGALORE -s KARNATAKA District: BANGALORE State: KARNATAKA Country: INDIA
From the above code, we can see the advantages of using the flag in the command line. Now we know that we do not have to pass the command line arguments in a sequence. Instead, we can use flag.
Conclusion
In this article, we have learned how to use command-line arguments in bash script in Linux. In addition, various special variables help to parse the command line arguments. So, using the command line argument we can do better and more efficient bash scripting.
- Related Articles
- How to Pass Command Line Arguments to Bash Script
- Parse Command Line Arguments in Bash on Linux
- How to Parse Command Line Arguments in C++?
- How to add command line arguments in Python?
- How to Package a Command Line Python Script
- How to Use cd Command in Bash Scripts
- How to Clear BASH Command Line History in Linux?
- Command line arguments in Java
- Command Line arguments in C#
- Command Line arguments in Lua
- Command Line Arguments in Python
- Java command line arguments
- Bash Function & How to Use It {Variables, Arguments, Return}
- Command line arguments example in C
- Command line arguments in C/C++
