Site icon FahmidasClassroom

How to use all command-line arguments in bash

Cmd

Different types of values can be passed to the bash script at the time of executing the script by using arguments. These arguments are passed through the command line. The values of all bash arguments can be printed in multiple ways. You can also print all bash arguments after the particular position.

The ways of printing all command-line arguments and the arguments after the particular position have been shown in this tutorial.

Different examples to parse all or the  particular command-line arguments:

Loops are mainly used to read command-line arguments separately in bash. Many other ways also exist in bash to read command-line arguments. Using the $* and $@ are the most common ways to read all bash arguments by using a ‘for’ loop. Without these expressions ‘shift’ command and array can be used to parse command-line arguments. Multiple ways of parsing command-line arguments have been shown in this part of the tutorial.

Parse arguments using $*:

The use of the `$*` expression without any quotes to read all bash arguments using the ‘for’ loop has been shown in the following script. It splits the argument values based on the space. The $even has been used in the script to count total even numbers and the $odd has been used to count total odd numbers in the arguments.

bin/bash
echo "Output of \$*"
#Define two counter values
even=0
odd=0
#Iterate the argument values using $*
for argval in $*; do
    #Check whether the argument value is even or not
    if [[ `expr $argval % 2` == 0 ]]; then
       ((even++))
    else
       ((odd++))
    fi
done
#Print total even numbers
echo "Total even numbers in arguments: $even"
#Print total odd numbers
echo "Total odd numbers in arguments: $odd"

According to the following output, there are two 3 even numbers (4, 34, and 8) and 2 odd numbers (7 and 91) in the command-line arguments.

Parse arguments using $@

The use of the $@ expression with double quotes to read all bash arguments using the ‘for’ loop has been shown in the following script. It splits the argument values based on the space but if any argument contains multiple words that are enclosed with quotes then that argument will be taken as a single argument. Here, the first two argument values will be stored into two variables named $name and $age that are printed later.

#!/bin/bash
echo "Output of \$@":
#Set the counter value
counter=1
#Iterate the argument values using $@
for argval in $@; do
    if [[ $counter == 1 ]]; then
         name=$argval
    elif [[ $counter == 2 ]]; then
         age=$argval
    fi
    ((counter++))
done
#Print the first argument value
echo "Name-$name"
#Print the second argument value
echo "Age-$age"

According to the following output, two argument values ‘Meena’ and 25 have been printed.

Parse arguments using the `shift` command:

The `shift` command can be used to read each command-line argument step by step. In the following code, each command-line argument has been read by the $1 variable and the argument position has been moved by the `shift` command.

#!/bin/bash
echo "The list of courses is:"
#Iterate the loop to print each argument value in a line
while (( "$#" )); do
  #The current argument value read by $1 variable
  echo $1
  #Go to the next argument position
  shift
done

According to the following output, three argument values have been passed at the time of the execution of the script.

Parse arguments after the particular argument position:

The particular number of command-line arguments can be parsed after the nth position by using the colon(:). The following script will print all argument values after the 2nd position.

#/bin/bash

#Store all command-line argument values in an array
args=("$@")
#total number of arguments
no_of_arguments=${#args[@]}

#Store all argument values after the 2nd position
argumentValues=${args[@]:2:$no_of_arguments}
#Print all argument values after the 2nd position
echo "The list of all argument values after the 2nd position:"
echo $argumentValues

According to the following output, 7 numeric values have been taken as the command-line argument values and the last 5 values have been printed.

Parse arguments as a single value:

You can use the $* or $@ expression with the `echo` command to read all bash arguments as a string. The $* expression has been shown in the following script to read all argument values as a string.

echo "All argument values are:"
#Print all argument values as a string
echo $*

According to the following output, the three argument values have been printed as a single value.

Parse arguments using argument variables:

The most simple way to read command-line argument values is to use an argument variable. The script name is read by the $0 variable and $1, $2, $3, and other variables are used to read the argument variables in sequential order. The following script will print the first three argument values.

#!/bin/bash
#Print argument values by using argument values
echo "The 1st argument value is $1"
echo "The 2nd argument value is $2"
echo "The 3rd argument value is $3"

According to the following output, three argument values, 89, 56, and 34 have been printed.

Conclusion  

Bash argument values are required to parse for different purposes in the bash script. This tutorial will help the bash users to learn different ways of reading all or the particular numbers of bash command-line arguments. You can check Command Line Arguments tutorial to know more details about the bash command-line arguments.

Exit mobile version