Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How To Pass Command Line Arguments in a Shell Script

    How To Pass Command Line Arguments in a Shell Script

    By RahulJuly 18, 20213 Mins Read

    Bash scripts are simple text files that contain a collection of commands. Bash scripts can help with administrative tasks, task automation, and executing multiple commands. They are used to automate recurring tasks/actions.

    Advertisement

    We can put all the commands that run on the terminal into a bash script and vice versa. Bash Scripts include imperative programming concepts like loops, conditionals, and functions.

    Command-Line Arguments are parameters that are specified with the filename of the bash script at the time of execution. The command-line arguments allow the script to perform dynamic actions based on the input:

    How to Pass an Argument to a Shell Script

    To pass a parameter to a bash script just write it after the name of the bash script in the terminal:

    ./my-test-script.sh argument 
    

    How to Pass Multiple Arguments to Shell Script

    You can also specify multiple arguments separated by space along with the name of the bash script file:

    ./my-test-script.sh arg_1 arg_2 arg_3............. arg_n 
    

    We can use the predefined variables to recall these arguments in the bash script. The first argument can be recalled by $1, the second by $2, and so on. The pre-defined variable “$0” refers to the bash script itself. The list of some other important predefined variables is given below:

    • [email protected] : Values of all arguments
    • $# :Total number of arguments
    • $$ : Process ID of the current shell

    Now we will use a bash file named animals.sh as an example.

    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
     
    echo "The animal in the first enclosure is: $1"
    echo "The animal in the second enclosure is: $2"
    echo "The animal in the third enclosure is: $3"
    echo "The total number of animals in the zoo are: $#"
    echo "The names of all the animals are: [email protected]"

    Now we will run this script in the terminal:

    Parse Command Line Arguments in Shell Script

    How to Pass an Argument Containing Space

    If an argument consists of multiple words that are separated by space then we need to enclose it in single quotes to pass it as a single argument:

    1
    2
    3
    #!/bin/bash
     
    echo "My name is: $1"

    Bash Command line Argument with White Space

    How to Pass Arguments with Special Characters

    We can also pass special characters as arguments. But they need to be written with backslashes before them.

    1
    2
    3
    #!/bin/bash
     
    echo "How much money did you spend today: $1"

    Shell Script Argument with Special Characters

    Passing Arguments Using Flags and Options in Shell Script

    Using flags and options is another common way of giving input to a bash script. An option is always followed by a value while flags are not followed by any value.

    First, we will make a new bash script that takes two different arguments (options) i.e. -n/--name for name, and -i/--id for an identification number.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #!/bin/bash
     
    ARGS=$(getopt -a --options n:i: --long "name:,id:" -- "[email protected]")
     
    eval set -- "$ARGS"
     
    while true; do
      case "$1" in
        -n|--name)
          name="$2"
          echo "Name: $name"
          shift 2;;
        -i|--id)
          identification="$2"
          echo "ID: $identification"
          shift 2;;
        --)
          break;;
      esac
    done

    In the code given above first, we created a variable that stores all the short and long names of our options. On the second line, we evaluated the ARGS variable. Then we used a while loop to assign a block of code to each option.

    In the code above shift is used to skip the values of the variables. The digit that follows the shift defines how many variables are skipped.

    Shell Script Argument with Flags

    Similarly

    Shell Script Argument with Full Word Flags

    Now we will add a -v flag which prints out a verbose menu:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #!/bin/bash
     
    ARGS=$(getopt -a --options n:i:v --long "name:,id:,verbose" -- "[email protected]")
     
    eval set -- "$ARGS"
     
    while true; do
      case "$1" in
        -n|--name)
          name="$2"
          echo "Name: $name"
          shift 2;;
        -i|--id)
          identification="$2"
          echo "ID: $identification"
          shift 2;;
        -v|--verbose)
          echo "Please use -n/--name or -i/--id to pass name or identification number respectively"
          break;;
        --)
          break;;
      esac
    done

    Flags are not followed by a colon when they are defined in a bash script because they do not take any argument with them unlike options e.g. the -n option takes a name with it such as Rahul.

    Shell Script Argumenet Example 7

    Shell Script Argumenet Example 8

    Conclusion

    A human operator carrying out recurring tasks manually is very inefficient. So such tasks should be automated. Scripts can help us do that.

    In this write-up, we have learned how to pass arguments through the command line to a bash script. Command-line arguments help us write dynamic scripts which perform different functions depending on the input.

    Moreover, we also discussed different types of arguments. We also touched a bit on predefined variables.

    arguments bash script shell
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    View 1 Comment

    1 Comment

    1. Ajit on July 15, 2016 6:18 am

      How many numbers of Argument we can pass in command line ?

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    • Understanding Reverse DNS: What it is and Why it Matters?
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.