How to pass multiple arguments in shell script with examples


Shell Scripting, Tips and Tricks

In my last article I shared the steps to delete content or element of an array from another array, now in this article I will share examples to pass multiple arguments in shell script. Now assuming you wish to write a shell script which needs multiple input arguments for performing the task. So it becomes little tricky to handle such scenarios. So to pass multiple parameters in shell script we will use while loop in our example.

How to pass multiple parameters in shell script in Linux

 

Steps to pass multiple parameters in shell script

This is our first script which only has a show_usage function which contains a list of input argument which the script will support. In this example we will use if condition to collect the input argument and perform the action accordingly.

#!/bin/bash
##
# @Description: Steps to pass multiple parameters in shell script
# Take single argument
##

function show_usage (){
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Options:\n"
    printf " -n|--number, Print number\n"
    printf " -s|--single [rpm_name], Print rpm version\n"
    printf " -m|--mdstat, Print /proc/mdstst (Update)\n"
    printf " -c|--collect, Collect rpm list to log file\n"
    printf " -t|--timeout, Collect timeout\n"
    printf " -p|--path, Provide the path\n"
    printf " -h|--help, Print help\n"

return 0
}

if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]];then
    show_usage
else
    echo "Incorrect input provided"
    show_usage
fi

So now for this script if we give --help or -h, the script will execute the show_usage function. Now even if you provide any other argument as an input, the script will assume that incorrect input was provided and will again execute show_usage function.

Output:

# /tmp/collect_input.sh -hh
Incorrect input provided
Usage: /tmp/collect_input.sh [options [parameters]]

Options:
-n|--number, Print number
-s|--single [rpm_name], Print rpm version
-m|--mdstat, Print /proc/mdstst (Update)
-c|--collect, Collect rpm list to log file
-t|--timeout, Collect timeout
-p|--path, Provide the path
-h|--help, Print help

Now next we will modify our script to use case instead of if. For such situations it is good to use case rather than if condition. Here the show_usage function remains the same.

case $1 in
     --number|-n)
         shift
         echo "You entered number as: $1"
         shift
         ;;
     --collect|-c)
         shift
         echo "You entered collect as: $1"
         ;;
     --timeout|-t)
        shift
        echo "You entered timeout as:"
         ;;
     *)
        show_usage
        ;;
esac

Now let us try to execute our script with single input argument

Output:

# /tmp/collect_input.sh -n 12
You entered number as: 12

So it worked, now let us try to execute our script with two input argument

# /tmp/collect_input.sh -n 12 -t 34
You entered number as: 12

As you see even if I gave two arguments the script was able to read only the first argument. To handle this we need to use a while loop.

while [ ! -z "$1" ]; do
  case "$1" in
     --number|-n)
         shift
         echo "You entered number as: $1"
         ;;
     --collect|-c)
         shift
         echo "You entered collect as: $1"
         ;;
     --timeout|-t)
        shift
        echo "You entered timeout as: $1"
         ;;
     *)
        show_usage
        ;;
  esac
shift
done

Here I have added an extra while loop which will re-run the case statement for all the input parameters provided to the shell script

Let us try to pass multiple parameters in shell script

# /tmp/collect_input.sh -n 12 -t 34
You entered number as: 12
You entered timeout as: 34

For three arguments

# /tmp/collect_input.sh -n 12 -t 34 -c value
You entered number as: 12
You entered timeout as: 34
You entered collect as: value

So now we are able to pass multiple parameters in shell script.

Lastly I hope the steps to pass multiple parameters in shell script on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

3 thoughts on “How to pass multiple arguments in shell script with examples”

  1. I am trying to call 2 methods

    makeDir(){
       mkdir vkumarTest
    
    }
    makeDir2(){
       mkdir migrationfolder
    }
    
    and calling like this
    
       --mkdir| -m)
            shift
            echo "create folder 1 $@"
            makeDir
            ;;
         --mkdir2| -m2)
           shift
           echo "create folder 2 $@"
           makeDir2
           ;;

    but only -m2 flag is able to create folder

    Reply
    • Use $1 instead of $@ as with $@ you are basically taking everything from input argument into your variable. It is a good idea to store $1 into a variable and then use it.

      Reply
  2. you can protect the last shift in the while loop with the code below, to avoid the
    “line ##: shift: shift count out of range” error/warning:

    if [ $# -gt 0 ]; then
    shift
    fi

    Reply

Leave a Comment