How to Pass all Arguments in Bash Scripting

Last updated: March 17, 2023

Bash arguments are one of the ways users provide inputs to the executing scripts. Using bash arguments, we create reusable scripts avoiding redundant code.

In this guide, we learn various approaches to passing all arguments in bash scripting.

Bash Pass all Arguments

Bash arguments are specified through the command-line after the script name. In the script to get the actual argument that was passed we use the dollar ($) sign, the syntax is $n, where n is an integer between 0 and 9. $0 is reserved for the script name, to get all bash arguments, we use either [email protected] or $*.

The [email protected] variable

In bash scripting, [email protected] represents the position of arguments, starting from one. For instance, when calling our script, we passed 4 and 7. Therefore, [email protected] will be equal to 4 7.

[email protected] is used to get all arguments individually quoted as “$1”, “$2”, ”$3”… The following bash script gets all arguments using [email protected] syntax.

# script name:

# this is the script name
echo "getting the script name."
echo "\$0 is $ARG0"
let SUM=$ARG1+$ARG2
echo "showing the sum"
echo "$ARG1 + $ARG2 = $SUM"

echo "getting all arguments using \[email protected]"
echo "[email protected]"
Pass all arguments using $@ variable

The output shows two arguments 4 and 7 that were passed via the script. In our script, we assume the arguments are numbers and we calculate the sum. In our example, the sum of 4 and 7 is 11 as shown, and [email protected] is 4 7.

The $* variable

When we want to store all bash arguments as a single string, we use $*. For instance, in our script above, $* will be equal to “4 7”.

$* gets all arguments as one string, that is, “$1 $2 $3…”. The separator is defined using the first character in $IFS (internal file separator). For instance, if IFS=”-=,#”, the separator will be “-”.

To check the IFS characters defined in your operating system, run the following command:

check the IFS characters

In our operating system, the first item, the separator, is an empty space (“ “) as shown in the above screenshot. We can change this by setting IFS to what we desire. 

In our sample script below, we change the IFS to “-” and the output is concatenated by a “-”.

# script name:


# print "$*"
echo "showing vars using \$*"
echo "value for \$IFS: $(set | grep ^IFS)"
echo "$*"
pass all arguments using $*

In the above screenshot, we set the first item of IFS to an hyphen(“-”). The output is concatenated with an hyphen when we use $* to process the arguments.

The following script shows the difference between $* and [email protected] For $* we can set a field separator using IFS.

# script name:

# setting the IFS operator
echo "all arguments using \[email protected]"
echo "[email protected]"
echo "all arguments using \$*"
echo "$*"
difference between $@ and $*

The screenshot above shows the difference between the $* and [email protected]


1. Bash Pass all Arguments as String

In bash script, to pass all arguments as string we use $*. As discussed earlier, we can set the IFS depending on our specific use case. The following script shows an example of passing all arguments as a string.

# script name:

# we use $* to get
echo "processing all args as string."
echo "$*"
pass all arguments as string

The output screenshot returns all arguments as a string using $*.

2. Read all Arguments using Loop

To read all bash arguments, we use loops. In this section, we are going to demonstrate reading all bash arguments using while and for loops.

$* with loop

The following scripts uses a for loop with “$*” to get all arguments as one string.

# script name:

for val in "$*"; do
 echo "value: $val"
$* with loop

The above script using a for-loop outputs the result as a one string.

[email protected] with loop

Now, with the same script, we use a while loop to read all arguments. The same construct will apply for a for loop with “[email protected]”. Let’s dig in:

The following script uses a while loop:

# script name:

while (( "$#" )); do
 echo "$1"

Here is the output of the above script:

$@ with loop

We will now use a for-loop to obtain the same result as above.

# script name:

for el in "[email protected]"; do
 echo "$el"

The output of the above script is the same as for the while loop. It is as follows:

$@ with for loop

The for loop gives the same output as the while loop with a counter and a shift operator. It prints each element one by one.

3. Using the shift command

All argument values can be read by `$#` and the `shift` command. The `$#` is used to count the total number of argument values and the `shift` command is used to move the position of the argument. The current argument value will be read by the $1 variable. The ‘while’ loop has been used here to read the argument values.

#Iterate the loop to read all argument values
while (( "$#" )); do
  #Print the current argument value
  echo "Argument-$counter: $1"
  #Move the argument position
using shift command

The value, ‘Hello, “how are you” everyone?’ has been splitted into three argument values like the “[email protected]” expression.

4. Bash Pass all Arguments as Array

An array is a sequence of characters or strings. We use [email protected] to get an array of arguments passed in, then we process them using a loop. This is useful when we want to perform a repetitive task on the provided input.

To process arrays in bash scripts, we use the following syntax.

# script name:

# array
[email protected]

for arg in $ARGS; do
 echo "$arg"

The output is as follows:

pass all arguments as array

We use a for loop to process the passed in arguments as an array.

5. Read all arguments after the nth position

The way of reading all arguments in bash after the nth position has been shown in the following script. All argument values have been stored in an array here and the length of the array has been counted to read all argument values after the first argument and after the third argument.


#Read all argument values into an array
argArray=("[email protected]")
#Count the total number of arguments

#Read all bash arguments after the first argument
#Print all arguments after the first argument
echo "All arguments after the first argument:$argument_list1"

#Read all bash arguments after the third argument
#Print all arguments after the third argument
echo "All arguments after the third argument:$argument_list2"
argument reading after nth position

The argument values, 8, 6, 7, 4, 3 and 2 have been given at the time of script execution. All argument values after the first argument are 6, 7, 4, 3 and 2 which have been printed in the first output.  All argument values after the third argument are 4, 3, and 2 which have been printed in the second output.

6. Bash All Arguments to use Arithmetic

The first argument is $0. Using [email protected] and $* gives us all arguments after the first. The arguments passed can be of any type, that is, integer or string. Therefore, as programmers, we must ensure to process and parse the arguments correctly.

For instance, to calculate the sum of three integers, we get arguments and calculate the sum. The following script illustrates this.

# script name:

# define the sum
for el in [email protected]; do
 let SUM+=$el
echo "the sum of [email protected] is: $SUM"

The output is as follows:

reading all argument - arithmetic operation

The above screenshot processes arguments after the first argument, the script name. The arguments we get are 3, 4, and 7. The script calculates the sum.


In this guide, we have demonstrated various ways to pass all arguments in bash with examples.

Another convenient way to handle command-line arguments is by using getopts which allows the script to specify which options are valid and what arguments they take.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment