Skip to main content
In this guide, we’ll dive into the core shell expansions available in POSIX-compliant shells (like Bash and Zsh). You’ll learn how to use:
  • Brace expansions
  • Parameter expansions
  • Command substitutions
  • Filename generation (globs)
These mechanisms let you generate sequences, extract substrings, capture command output, and match multiple filenames—streamlining your shell scripts and command-line workflows.

Table of Expansion Types

For an in-depth reference, see the Bash manual on Shell Expansions.

1. Brace Expansion

Brace expansion quickly generates arbitrary strings. It’s purely a string generation mechanism—no variables or globs involved.
Brace expansions must not be quoted for the shell to recognize them. For example, echo "{A,B}" will literally output {A,B}.

2. Parameter Expansion

Parameter expansion lets you inspect or transform variable values without invoking external commands.

Basic Variable Expansion

Removing Directory Components

To strip the longest matching prefix (e.g., remove everything up to the last slash), use ##*/:
Output:
Always quote your expansions (e.g., "${var}") to prevent word splitting and globbing in unexpected ways.

3. Command Substitution

Command substitution captures the stdout of a command and embeds it in another command’s arguments:

4. Filename Generation (Globbing)

Globbing uses wildcard patterns to match filenames:

Next Steps

Now that you’ve seen the major shell expansions, try combining them to simplify your scripts—generate file lists, parse log entries, or batch-rename files with a single command. For more examples and edge cases, consult the GNU Bash Reference Manual.