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:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Brace expansions
- Parameter expansions
- Command substitutions
- Filename generation (globs)
Table of Expansion Types
| Expansion Type | Description | Example |
|---|---|---|
| Brace | Generate comma- or range-separated strings | echo {A,B,C} → A B C |
| Parameter | Manipulate variable values (substrings, defaults) | ${var##*/} → strips longest */ prefix |
| Command Substitution | Insert command output into another command | echo "Date: $(date +%F)" |
| Filename Generation | Match files with wildcards (globs) | ls *.txt → lists all .txt files |
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##*/:
Always quote your expansions (e.g.,
"${var}") to prevent word splitting and globbing in unexpected ways.