Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»How To Pass Command Line Arguments in a Shell Script

    How To Pass Command Line Arguments in a Shell Script

    RahulBy RahulFebruary 15, 20153 Mins ReadUpdated:July 18, 2021

    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.

    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.

    Shell
    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:

    Shell
    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.

    Shell
    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.

    Shell
    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:

    Shell
    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
    Previous ArticleHow To SetUp Nginx Virtual Hosts on Ubuntu 18.04 & 16.04 LTS
    Next Article 5 Curl Commands to download Files

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    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

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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