Matching File Names with Shell Expansions on RHEL shows what command-line expansions are and provides some usage examples. We’re using Red Hat Enterprise Linux 10, but all the examples apply to any Linux distribution.
What are Command-Line Expansions?
When we type a command at the Bash shell prompt, the shell processes that command line through multiple expansions before running it. We can use these shell expansions to perform complex tasks that would otherwise be difficult or impossible.
The Bash shell can perform the following expansions:
- Pathname expansion, which helps to select one or more files by pattern matching.
- Brace expansion, which can generate multiple strings of characters.
- Tilde expansion, which expand to a path to a user home directory.
- Variable expansion, which replaces text with the value that is stored in a shell variable.
- Command substitution, which replaces text with the output of a command.
What “expansion” means:
When you type a command, Bash scans the line for special patterns such as ($VAR
, *
, {}
, ~
) and expands them into something else (such as values, filenames, lists, etc.) before running the command.
Think of it as:What you type
→ Expansion happens → What actually runs
Let’s provide a simple example now. If we execute:
cd ~
The Bash shell will expand it to:
cd /home/yourusername
We’ll explore each expansion type later!
Path Name Expansion (aka Globbing)
This is when Bash automatically expands wildcards into matching filenames in the current directory.
Think of it as:
You type a pattern → Bash expands it into a list of matching file/directory names → The command runs on that list.
If there’s no match, the behavior depends on your settings (by default, the pattern stays literal unless you enable nullglob
).
Table of common Patterns and their respective match:
Pattern | Matches |
---|---|
* | Any string of zero or more characters |
? | Any single character |
[abc…] | Any one character in the enclosed class (between the square brackets) |
[!abc…] | Any one character not in the enclosed class |
[^abc…] | Any one character not in the enclosed class |
[[:alpha:]] | Any alphabetic character |
[[:lower:]] | Any lowercase character |
[[:upper:]] | Any uppercase character |
[[:alnum:]] | Any alphabetic character or digit |
[[:punct:]] | Any printable character that is not a space or alphanumeric |
[[:digit:]] | Any single digit from 0 to 9 |
[[:space:]] | Any single white space character, which might include tabs, newlines, carriage returns, form feeds, or spaces |
Pattern Matching vs Pathname Expansion:
- Pattern Matching is the syntax (like
*
,?
,[abc]
) that describes sets of filenames. - Pathname Expansion is the process Bash uses to take those patterns and expand them into actual filenames.
So the patterns are the “rules”, and pathname expansion is Bash applying those rules to your directory contents.
Example 1 – Using *
ls *.sh
Expands to:
script1.sh script2.sh test.sh
(but not file1.txt and file2.txt - it only matches any files ended with .sh)
Example 2 – Using ?
ls file?.txt
Expands to:
file1.txt fileA.txt
(but not file10.txt - it only matches file names started with file + one more character)
Example 3 – Using [abc]
ls file[ab].log
Expands to:
filea.log fileb.log
(but not filec.log - it only matches file names started with file and followed with "a" or "b")
Example 4 – Negating [!x]
ls file[!1].txt
Expands to:
file2.txt file3.txt
(but not file1.txt)
Brace Expansion
Brace expansion in Bash is a way to generate multiple strings from a pattern by enclosing comma-separated items or ranges in {}
— before any other expansions happen.
It’s not about matching files (like wildcards), but about creating strings.
Think of it as a “string generator”, not a “file matcher.”
Example 1:
echo file{1..3}.txt
Expands to:
file1.txt file2.txt file3.txt
(This is not pathname expansion; it literally generates the names.)
Example 2:
mkdir /tmp/{test1,test2,test3}
Expands to:
/tmp/test1 /tmp/test2 /tmp/test3
(It will create the directories test1, test2, and test3 under the /tmp)
Tilde Expansion
Tilde expansion in Bash means the ~
symbol automatically expands to a home directory:
~
→ your home directory~user
→ that user’s home directory
Examples:
cd ~ # goes to /home/yourusername
ls ~ # lists your home directory
cd ~john # goes to /home/john (if exists)
Variable Expansion
A variable acts like a named container that stores a value in memory. Variables simplify accessing and modifying stored data, whether from the command line or within a shell script.
You can assign data as a value to a variable with the following syntax:
VARIABLENAME=value
Variable expansion in Bash means replacing a variable name with its stored value.
Examples:
name="Thor"
echo $name # Thor
echo $HOME # /home/yourusername
echo "Hi, $USER" # Hi, yourusername
To prevent mistakes due to other shell expansions, you can put the name of the variable in curly braces, for example${VARIABLENAME}
:
user@host:~$USERNAME=operator
user@host:~$echo ${USERNAME}
operator
Important: Variable names can contain only letters (uppercase and lowercase), numbers, and underscores. Variable names are case-sensitive and cannot start with a number!
Command Substitution
Command substitution enables the output of a command to replace the command itself on the command line. Command substitution occurs when you enclose a command in parentheses and precede it by a dollar sign ($
).
The $(
form can nest multiple command expansions inside each other.command
)
For example:
user@host:~/glob$ echo Today is $(date +%A).
Today is Wednesday.
user@host:~/glob$ echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.
Note: An earlier form of command substitution uses backticks: `command`. Although the Bash shell still accepts this format, try to avoid it because you might visually confuse backticks with single quotation marks, and backticks cannot be nested.
Protecting Arguments from Expansion
Many characters have a special meaning in the Bash shell. To prevent shell expansions on parts of your command line, you can quote and escape characters and strings.
The backslash (\
) is an escape character in the Bash shell.
For example:
user@host:~/glob$ echo The value of $HOME is your home directory.
The value of /home/user is your home directory.
user@host:~/glob$ echo The value of \$HOME is your home directory.
The value of $HOME is your home directory.
In the preceding example, with the dollar sign protected from expansion (protected by the backslash \), Bash treats it as a regular character and does not expand (does not show the variable value) the $HOME
variable!
Note: To protect longer character strings, you can use single quotation marks ('
) or double quotation marks ("
) to enclose strings. They have slightly different effects. Single quotation marks stop all shell expansion. Double quotation marks stop most shell expansion.
Double quotation marks suppress special characters other than the dollar sign ($
), backslash (\
), backtick ( ` ), and exclamation point ( ! ) from operating inside the quoted text. Double quotation marks block pathname expansion, but still allow command substitution and variable expansion to occur:
user@host:~/glob$ myhost=$(hostname -s); echo $myhost
host
user@host:~/glob$ echo "***** hostname is ${myhost} *****"
***** hostname is host *****
Use single quotation marks to interpret all text between the quotes literally – the Bash will not expand the variable or the command substitution:
user@host:~/glob$echo "Will variable $myhost evaluate to $(hostname -s)?"
Will variable host evaluate to host?
user@host:~/glob$echo 'Will variable $myhost evaluate to $(hostname -s)?'
Will variable $myhost evaluate to $(hostname -s)?
That’s it for now 🙂