Posted on
Questions and Answers

Reference positional parameters beyond `$9` using `${10}` (no `shift` required)

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Understanding and Using Positional Parameters Beyond $9 in Bash Scripts

What are positional parameters in Bash scripting?

Positional parameters are variables in a bash script that hold the value of input arguments passed to the script when it is executed. These parameters are automatically assigned by Bash and are named $1, $2, $3, etc., corresponding to the first, second, third, and subsequent arguments.

How do you normally access these positional parameters?

In a Bash script, you can directly access the first nine parameters using $1 to $9. For example, $1 retrieves the first argument passed to the script, $2 the second, and so on.

What if there are more than nine parameters? How do I access the tenth parameter and beyond?

Beyond the ninth parameter ($9), you cannot directly use $10 to refer to the tenth parameter as Bash interprets it as ${1}0 (i.e., the first parameter followed by a literal '0'). To access the tenth and subsequent parameters, you need to use curly braces to delimit the parameter number, like ${10}, ${11}, etc.

Can you provide a simple example illustrating the use of parameters beyond $9?

Certainly! Let's assume you have a Bash script named multi_input.sh, and you want to print the 10th argument passed to it. You would use ${10} inside your script as follows:

#!/bin/bash
echo "The tenth argument is: ${10}"

If you run this script like this:

bash multi_input.sh 1 2 3 4 5 6 7 8 9 10 11 12

It will output:

The tenth argument is: 10

What if I need a practical demonstration with a more complex script?

Let’s create an executable script that demonstrates using multiple positional parameters, particularly focusing on those beyond $9. This script will summarize shopping costs provided via command-line arguments:

#!/bin/bash

# Define a function to calculate total shopping cost
calculate_total() {
    local total=0
     # Loop through all the arguments provided
    for price in "$@";
    do
        total=$((total + price))
    done
    echo "Total shopping cost is: \$$total"
}

# Call the function with all passed parameters
calculate_total "$@"

Save this script as total_shopping_cost.sh, make it executable with chmod +x total_shopping_cost.sh, and run it like this:

./total_shopping_cost.sh 25 15 30 10 20 5 40 10 50 70 40 30

It should output the total of these amounts.

Summary and Conclusion

Positional parameters are a fundamental aspect of Bash scripting, allowing scripts to be dynamic and interactive. However, when dealing with more than nine parameters, it is crucial to use the ${10}, ${11}, etc., syntax to correctly reference them. Understanding and utilizing these parameters can greatly enhance the functionality of your Bash scripts, making them more flexible and capable of handling a variety of inputs. This bit of knowledge is especially useful for scripts requiring extensive user input or processing multiple command line arguments.

Further Reading

For a deeper dive into Bash scripting with positional parameters, consider exploring the following resources:

  • Advanced Bash Scripting Guide: Positional Parameters Advanced Bash Scripting Guide - This guide provides a comprehensive look at using positional parameters and includes complex scripting examples.

  • Using Positional Parameters - IBM Developer IBM Developer: Positional Parameters - A clear explanation of positional parameters with practical examples and common pitfalls.

  • Manipulating Strings in Bash Manipulating Strings in Bash - Learn about string manipulation in Bash scripts which complements understanding of positional parameters.

  • Bash Scripting Tutorial - Ryans Tutorials Ryans Tutorials on Bash - A tutorial that covers basics to advanced topics including detailed sections on parameters and other crucial scripting fundamentals.

  • Effective Shell Programming - Positional Parameters Effective Shell - Focused on best practices and efficient use of positional parameters within scripts for automation and everyday tasks.

These resources will expand your knowledge and skills in Bash scripting, specifically in handling multiple and complex input scenarios.