How to pass arguments in Bash script with examples

Bash scripting is a powerful tool for automating tasks on Unix-like systems. One of the fundamental aspects of bash scripting is handling command-line arguments and options. These enable scripts to be more flexible and interactive, allowing users to pass specific instructions or data to the script. This article aims to guide you from the basics to more advanced techniques of argument and option handling in Bash, suitable for a range of audiences from beginners to advanced users.

In this tutorial you will learn:

  • How to handle positional parameters in Bash scripts
  • Using special variables for argument processing
  • Working with the ‘shift’ command for advanced argument processing
  • Basic and advanced uses of ‘getopts’ for option parsing
  • Best practices for handling script inputs
How to pass arguments in Bash script with examples
How to pass arguments in Bash script with examples

Understanding Command-Line Arguments

Before diving into complex scripting, it’s important to understand the basics of how command-line arguments are passed and accessed in Bash scripts. This section covers simple ways to access and use these arguments.

  1. Basic Positional Parameters: Accessing arguments passed to the script using positional parameters.
    #!/bin/bash
    echo "Script Name: $0"
    echo "First Argument: $1"
    echo "Second Argument: $2"

    In this example, `$0` represents the script’s name, and `$1`, `$2`, etc., represent the arguments passed to the script.

    Basic Positional Parameters
    Basic Positional Parameters
  2. Handling Multiple Arguments: Using a loop to process multiple arguments.
    #!/bin/bash
    for arg in "$@"
    do
        echo "Argument: $arg"
    done

    This script will loop through each argument passed to it and print it out. It demonstrates the use of `$@` to access all arguments as a list.

    Handling Multiple Arguments
    Handling Multiple Arguments
  3. Using Special Variables: Exploring special variables like `$#`, `$*`, and `$?`.
    #!/bin/bash
    echo "Number of arguments: $#"
    echo "All arguments: $*"
    echo "Exit status of last command: $?"

    This script shows how to use special variables: `$#` for the number of arguments, `$*` for all arguments as a single string, and `$?` for the exit status of the last executed command.

    Using Special Variables
    Using Special Variables


  4. Advanced Argument Processing with Shift: Using the ‘shift’ command to process arguments sequentially.
    #!/bin/bash
    while [ "$#" -gt 0 ]; do
        echo "Processing parameter: $1"
        shift
    done

    This example demonstrates the use of the `shift` command to sequentially process each argument passed to the script.

    Advanced Argument Processing with Shift
    Advanced Argument Processing with Shift
  5. Basic getopts Usage: Parsing options with ‘getopts’.
    #!/bin/bash
    while getopts "ab:" opt; do
      case $opt in
        a) echo "Option -a was triggered." ;;
        b) echo "Option -b was triggered, Argument: $OPTARG" ;;
      esac
    done

    This script uses `getopts` to parse options `-a` and `-b`, where `-b` requires an associated value.

    Basic getopts Usage
    Basic getopts Usage
  6. Advanced getopts with Long Options: Handling both short and long options using ‘getopts’.
    #!/bin/bash
    while getopts "ab:-:" opt; do
      case $opt in
        a) echo "Alpha mode active" ;;
        b) echo "Beta mode set to '$OPTARG'" ;;
        -)
          case "${OPTARG}" in
            help) echo "Help option selected." ;;
            *) echo "Unknown option --${OPTARG}" ;;
          esac
        ;;
      esac
    done

    This more complex script extends the functionality of `getopts` to handle long options like `–help`.

    Advanced getopts with Long Options
    Advanced getopts with Long Options


Conclusion

Mastering the handling of command-line arguments and options is crucial for creating flexible and powerful Bash scripts. Starting from simple positional parameters to advanced `getopts` usage, these techniques allow your scripts to interactively respond to user inputs. As you progress in your scripting journey, these skills will form the foundation for more complex script development and automation tasks.

 



Comments and Discussions
Linux Forum