Bash Positional Parameters and Brace Expansion Tricks

Bash, the powerful command-line interface for Linux and Unix systems, offers a wealth of features for handling command-line arguments and manipulating variables. In this post, we’ll delve into positional parameters, which represent arguments passed to your script, and explore how brace expansion can unlock advanced scripting capabilities.

Understanding Positional Parameters

When you run a Bash script, any words you type after the script name become positional parameters. These are numbered sequentially, starting with $1, $2, and so on. You can access these parameters within your script for dynamic behavior.

Brace Expansion: Your Scripting Swiss Army Knife

Brace expansion, denoted by curly braces {}, is a versatile tool for generating sequences, expanding variables, and performing manipulations. Let’s dive into some common use cases:

1. Multiple-Digit Parameters

For positional parameters beyond $9, you need to use braces:

echo ""  # Access the 10th parameter

2. Indirection with !

Indirection allows you to access a variable whose name is stored in another variable:

X=ABC
ABC="Hello, world!"
echo "${!X}"   # Output: Hello, world!

3. Default Values and Parameter Checks

echo "${VAR:-default}"   # If VAR is unset or null, use "default"
echo "${VAR:=default}"   # If VAR is unset or null, set it to "default" and use it
echo "${VAR:?Error: VAR is unset or null}"  # Exit with an error if VAR is unset or null

4. String Manipulation

STR="Hello, world!"
echo "${STR:1:4}"    # Output: ello (substring starting at index 1, length 4)
echo "${STR#*, }"    # Output: world! (remove everything up to the first comma and space)
echo "${STR%.SH}"    # Output: /usr/local/bin/hotdog (remove suffix ".SH")

Example Script: Positional Parameter Handling

#!/bin/bash

echo "Arg 1 is $1"
echo "Arg 11 is "

shift  # Shift parameters to the left

echo "Now Arg 1 is $1"
echo "Now Arg 11 is "

echo "Script name: $0" 

Output

Arg 1 is A
Arg 11 is K
Now Arg 1 is B
Now Arg 11 is L
Script name: ./script.sh

Explanation: The script showcases the use of $1 (the first parameter) and ${11}. The shift command moves the parameters (Arg 1 is now B since A was shifted out).

Example: String Manipulation

#!/bin/bash
filename=/usr/local/bin/example.sh
basename=${filename##*/}  # Remove everything up to the last slash
echo "Basename: $basename"
extension=${filename##*.}  # Remove everything up to the last dot
echo "Extension: $extension"

Output:

Basename: example.sh
Extension: sh

Why Use Positional Parameters and Brace Expansion?

  • Dynamic Scripts: Handle varying inputs and adapt script behavior accordingly.
  • Concise Code: Brace expansion offers shortcuts for common operations.
  • Robustness: Parameter checks prevent unexpected script failures.

Feel free to experiment with these techniques and unlock the full potential of Bash scripting!

Leave a Reply

Your email address will not be published. Required fields are marked *