Advanced Bash Scripting

Expansions Part One

Parameter Part One

In Bash scripting, parameter expansion lets you transform variable values using patterns inside ${}. Previously, we replaced “file.txt” with “data” in a path:

#!/usr/bin/env bash
path="/home/user/file.txt"
echo "${path/file.txt/data}"
# Output: /home/user/data

In this lesson, we’ll focus on pattern matching and how to remove prefixes and suffixes from strings using Bash parameter expansion.

Patterns in Bash

A pattern is a sequence of characters recognized by the shell for matching or substitution. You’ll find patterns in file globbing (e.g., *.txt), text processing, and data validation. In Bash, patterns power parameter expansion, letting you transform variable content without external tools.

Variable Expansion vs. Parameter Expansion

By default, ${var} simply expands to its value:

name="John Doe"
echo "Hello, ${name}"
# Hello, John Doe

When you include operators like # or % inside the braces, Bash invokes parameter expansion, applying pattern-based modifications to the variable’s value.

Note

Always enclose the expression in double quotes ("${...}") to preserve spaces and prevent word splitting.

Removing Prefixes and Suffixes

Bash supports removing the shortest matching prefix or suffix from a string with ${var#pattern} and ${var%pattern}.

Understanding Prefix and Suffix

Consider these job titles in an IT company:

The image lists different engineering job titles: Mid DevOps Engineer, Jr. Software Engineer, Sr. DevOps Engineer, and Associate DevOps Engineer, alongside an icon of a briefcase with a magnifying glass.

  • Prefixes (associate, senior, junior, mid) indicate tenure or level.
  • The suffix (Engineer) indicates the job field.

Prefix Removal with

The # operator deletes the shortest matching pattern from the start of the string:

The image illustrates the concept of "Parameter Expansions" with a focus on removing a prefix, represented by a hashtag symbol and scissors icon.

Example 1: Remove the leading H:

greetings="Hello World"
echo "${greetings#H}"
# Output: ello World

If the pattern isn't found at the beginning, the value remains unchanged:

echo "${greetings#e}"
# Output: Hello World

Example 2: Remove the word “Hello ” (including the space):

echo "${greetings#Hello }"
# Output: World

Patterns are case-sensitive: "h" won’t match "H".

echo "${greetings#h}"
# Output: Hello World

Suffix Removal with %

The % operator removes the shortest matching pattern from the end of the string:

Example 1: Drop the trailing d:

echo "${greetings%d}"
# Output: Hello Worl

Example 2: Remove “rld” at the end:

echo "${greetings%rld}"
# Output: Hello Wo

If the suffix doesn't match exactly, the string stays the same:

echo "${greetings%world}"
# Output: Hello World

Warning

Pattern matching in Bash is case-sensitive. Ensure your pattern matches the exact case of the prefix or suffix.

Quick Reference Table

OperatorActionExample
${var#pattern}Remove shortest prefix match${greetings#Hello }World
${var%pattern}Remove shortest suffix match${greetings%rld}Hello Wo

The image illustrates parameter expansions, showing a blue circle with a hash symbol for "Prefix" and a red circle with a percent symbol for "Suffix."

Next Steps

In the next lesson, we'll explore longest-match removals using ## and %% to strip more complex patterns.

References

Watch Video

Watch video content

Previous
Variables