> ## 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.

# Overview

> This guide explores core shell expansions in POSIX-compliant shells to enhance shell scripts and command-line workflows.

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

| 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       |

For an in-depth reference, see the [Bash manual on Shell Expansions][bash-expansion].

[bash-expansion]: https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html

***

## 1. Brace Expansion

Brace expansion quickly generates arbitrary strings. It’s purely a string generation mechanism—no variables or globs involved.

```bash theme={null}
# Generates A, B, C
echo {A,B,C}

# Generates numbers 1 through 5
echo {1..5}
```

<Callout icon="lightbulb" color="#1CB2FE">
  Brace expansions must not be quoted for the shell to recognize them. For example, `echo "{A,B}"` will literally output `{A,B}`.
</Callout>

***

## 2. Parameter Expansion

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

### Basic Variable Expansion

```bash theme={null}
USER_HOME=$HOME
echo "Your home directory is: $USER_HOME"
```

### Removing Directory Components

To strip the longest matching prefix (e.g., remove everything up to the last slash), use `##*/`:

```bash theme={null}
#!/usr/bin/env bash

some_script="/usr/bin/my_script.sh"
# Remove the longest match of '*/' from the front
echo "${some_script##*/}"
```

Output:

```bash theme={null}
$ ./expansions.sh
my_script.sh
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Always quote your expansions (e.g., `"${var}"`) to prevent word splitting and globbing in unexpected ways.
</Callout>

***

## 3. Command Substitution

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

```bash theme={null}
current_date=$(date +%Y-%m-%d)
echo "Today is $current_date"
```

***

## 4. Filename Generation (Globbing)

Globbing uses wildcard patterns to match filenames:

```bash theme={null}
# List all .log files
ls *.log

# Recursive match in subdirectories (with Bash extglob)
shopt -s globstar
echo **/*.md
```

***

## 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][bash-expansion].

```markdown theme={null}

<CardGroup>
<Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-bash-scripting/module/0e090d75-12b5-4e0f-ace8-519f11d7b5d2/lesson/017ed46b-54db-44fc-a839-6627eec48490"/>
</CardGroup>
```
