Shell Scripting Tutorial for Beginners 4 - Pass Arguments to a Bash-Script

3 min read 2 hours ago
Published on Nov 06, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial aims to guide beginners on how to pass arguments to a Bash script in Linux. Understanding how to use arguments is essential for creating dynamic and flexible shell scripts. By the end of this tutorial, you'll be able to write scripts that can accept input directly from the command line.

Step 1: Understanding Positional Parameters

Positional parameters are special variables in Bash that hold the values of the arguments passed to a script. Here’s how they work:

  • $0: Refers to the name of the script itself.
  • $1, $2, ... $n: Refer to the first, second, ..., nth argument passed to the script.
  • $#: Represents the total number of arguments supplied.
  • $*: Returns all arguments as a single string.
  • $@: Returns all arguments as separate strings.

Practical Tip

Always remember that argument counting starts from 1, not 0. This means $1 is the first argument you provide.

Step 2: Creating a Simple Bash Script

To practice passing arguments, create a basic Bash script. Follow these steps:

  1. Open your terminal.
  2. Create a new file named example_script.sh:
    touch example_script.sh
    
  3. Open the file in a text editor (like nano):
    nano example_script.sh
    
  4. Add the following content to the script:
    #!/bin/bash
    echo "Script name: $0"
    echo "Total arguments: $#"
    echo "First argument: $1"
    echo "All arguments as a single string: $*"
    echo "All arguments as separate strings: $@"
    
  5. Save and exit the editor.

Practical Advice

Make sure to include #!/bin/bash at the beginning of your script. This line tells the system to execute the script using the Bash shell.

Step 3: Running the Script with Arguments

Now that you have created the script, it’s time to run it with some arguments:

  1. In your terminal, make the script executable:
    chmod +x example_script.sh
    
  2. Run the script with arguments:
    ./example_script.sh arg1 arg2 arg3
    

Common Pitfall

If you forget to make the script executable, you will encounter a "permission denied" error when trying to run it.

Step 4: Accessing Arguments Greater than Nine

If you need to access arguments beyond the ninth, use braces {}:

  • For the 10th argument, use ${10}.
  • For the 11th argument, use ${11}, and so on.

Alternatively, you can use the shift command to manipulate positional parameters. Each time you run shift, it shifts all parameters to the left, allowing you to access parameters greater than 9 easily.

Example

To access the 10th argument after using shift:

shift 9
echo "Tenth argument: $1" # Now this will refer to the original $10

Conclusion

In this tutorial, you learned how to pass arguments to a Bash script and access them using positional parameters. By understanding how to utilize $0, $1, $#, $*, and $@, you can create flexible scripts that react to user input. As a next step, consider experimenting with more complex scripts that involve conditional statements and loops while utilizing arguments.