This will introduce how to pass and use command line arguments in Bash when you execute the Bash script in terminal.
1. Pass and use Command Line arguments
You can run the script without any arguments at the terminal as follows,
$ sh script.sh
But, you can pass arguments form the command.
$ sh script.sh 111 222 333
When an argument is passed, the script can read the argument in the form of $1
, $2
, $n
inside the script. Here, n
means the order in which the argument is entered. And $#
is the number of arguments passed.
#!/bin/bash
echo "1st param = $1"
echo "2nd param = $2"
echo "3rd param = $3"
echo "The number of params = $#"
Output:
$ bash example.sh 111 222 333
1st param = 111
2nd param = 222
3rd param = 333
The number of params = 3
If executed without arguments, the number of arguments is 0 and the arguments are null (empty).
$ bash example.sh
1st param =
2nd param =
3rd param =
The number of params = 0
2. Parsing arguments
You can assign the arguments to different variables as follows.
#!/bin/bash
num1=$1
num2=$2
num3=$3
sum=$(($num1+$num2+$num3))
echo "num1 = $num1"
echo "num2 = $num2"
echo "num3 = $num3"
echo "sum = $sum"
Output:
$ bash example.sh 111 222 333
num1 = 111
num2 = 222
num3 = 333
sum = 666
3. Parsing arguments with for loop
If the number of arguments passed to the script is not predetermined each time it is run, a for loop can be used to read all the arguments of a dynamic number.
$@
is a string containing all the arguments passed, with each argument separated by a space.
The following for loop can be used to read all the arguments passed.
#!/bin/bash
echo "All params = $@"
echo "Loop all params!"
for i in $@
do
echo "$i"
done
Output:
$ bash example.sh 111 222 333
All params = 111 222 333
Loop all params!
111
222
333
4. Check if arguments passed (1)
[ -z $A ]
returns True when the length of the A string is 0.
So, You can check if an argument is passed as follows.
#!/bin/bash
if [ -z $1 ]
then
echo "No parameter passed"
else
echo "Parameter passed = $1"
fi
Output:
$ sh ex_arg4.sh 111
Parameter passed = 111
$ sh ex_arg4.sh
No parameter passed
5. Check if arguments passed (2)
[ -n "$A" ]
returns False when the length of the string A is 0. It returns the opposite value of -z
.
With this, You can check if an argument is passed as follows.
if [ -n "$1" ]
then
echo "Parameter passed = $1"
else
echo "No parameter passed"
fi
Output:
$ bash example.sh 111
Parameter passed = 111
$ bash example.sh
No parameter passed
Related Posts
- How to measure elapsed time in Bash
- Ceil, Floor, Round off a devided number in Bash
- Check if a number is Positive or Negative in Bash
- How to Print a Variable in Bash
- Number Comparison Operators in Bash
- Convert between Uppercase and Lowercase in Bash
- Check if a File or Directory exists in Bash
- How to extract substring in Bash
- Bash Case Statement with examples
- How to use Command Line Arguments in Bash
- How to use Arrays in Bash
- How to Compare strings in Bash Shell
- if..else statement in Bash
- Add, Subtract, Multiply, Division with Numbers in Bash
- Bash For, Whlie Loop Examples
- How to Pass and Use command line arguments in Bash