Reading Multiple Arguments with For or While loop

We can use “@” variable to access every parameter passed to the script via the command line. It is a special variable that holds the array of variables in BASH. In this case, we are using it alone, so it contains the array of positional parameters passed in. We can use it to iterate over the parameters passed using loops or while loop as well.

#!/bin/bash

for i in $@
do 
        echo -e "$i\n"
done

Using loops and @ variable to access the parameters as array elements.

We used a range-based for loop to iterate over till there are elements in the @ array. We simply iterate over the array and print the element. We can simply assign it, modify the values, and make the required changes to the parameters and arguments to achieve the desired outcome from the script. 

OR

We can also print the arguments using the while loop and the environmental variables of BASH.

#!/bin/bash

i=$(($#-1))
while [ $i -ge 0 ];
do
    echo ${BASH_ARGV[$i]}
    i=$((i-1))
done

Using while loop to iterate over the passed parameters.

We are using the variable ‘#‘ as it holds the number of parameters passed in. We initialize the number of parameters and take away one as we are going to use an array to iterate over it. So, as usual, the array’s index starts from 0. As this array is initialized from the last element or parameter passed, we need to decrement the counter until 0 to print every parameter in the order it is passed. We simply use the BASH_ARGV array to access the parameters and print its value. Also, at every iteration, we decrease the value of i- the iterator or counter by one using the arithmetic double braces.  From this, we simply print every parameter passed to the script using a while loop as shown from the output screenshot. 

How To Pass and Parse Linux Bash Script Arguments and Parameters

Parsing and Passing of Arguments into bash scripts/ shell scripts is quite similar to the way in which we pass arguments to the functions inside Bash scripts. We’ll see the actual process of passing on the arguments to a script and also look at the way to access those arguments inside the script.

Similar Reads

Passing arguments before running

We can pass parameters just after the name of the script while running the bash interpreter command. You can pass parameters or arguments to the file. Just the command for running the script normally by adding the value of the parameters directly to the script. Every parameter is a space-separated value to pass to the shell script....

Detecting Command Line Arguments

Now, we’ll see how we access those parameters inside of the script. We’ll use the number of the parameters passed in the order i.e for the first parameters passed, we’ll parse(access) the parameter by using $1 as the variable. The first parameter is stored in the $1 variable. Furthermore, you can assign this variable to any other user-defined variable you like. For the nth parameter passed, you can use $n to access that particular parameter. Here, the variable name starts with 1 because the filename/ script name is the 0th parameter. If you have more than 9 parameters, make sure to use { } around the number as without the parenthesis, bash will only see $10 as $1 and exclude the 0, so use ${10} and so on instead of simply $10....

Assign Provided Arguments To Bash Variable

We can also assign it to other custom variables to make the script more dynamic and mold it according to the needs. Though the above script when run will only print two parameters, surely you can access more parameters using the variable as the order of parameters in numbers. The script can access the positional variables from the command line and use them in the required places wherever needed within the script....

Reading Multiple Arguments with For or While loop

We can use “@” variable to access every parameter passed to the script via the command line. It is a special variable that holds the array of variables in BASH. In this case, we are using it alone, so it contains the array of positional parameters passed in. We can use it to iterate over the parameters passed using loops or while loop as well....

Reading With Parameter Names

Using getopts to parse arguments and parameters...

Contact Us