Generating Dynamic Pathnames
5 min read·Jan 1, 2025
The shell expansion refers to the process by which the shell interprets certain special characters or character sequences and expands them into treatable command parameters before executing a command.
In Bash, there are essentially three expansions for dynamically generating pathnames based on patterns:
- The tilde expansion
- The wildcard expansion
- The brace expansion
The tilde expansion
In Bash, the tilde character ~ is a shortcut for the path to the home directory.
It is used to write relative pathnames, starting from the home directory, regardless of the current working directory.
Example
For example, assuming the current user account is johndoe, the following command will change the current working directory to /home/johndoe/Documents on Linux and /Users/johndoe/Documents on macOS:
$ cd ~/Documents
The wildcard expansion
The wildcard expansion, also known as globbing, is a mechanism that allows you to specify patterns that match filenames for commands like cd or ls.
Common wildcard characters include:
?: will match any single character.*: will match zero or more characters.[]: match any single character within the specified set[<char>...]or range[<start>-<end>].
Example
Let's consider the files in the current directory:
$ ls
backup.sql candidates.csv data.csv
draft_1.txt draft_2.txt notes.txt
report.csv
This command will output the names of the files with a .txt extension:
$ echo *.txt
draft_1.txt draft_2.txt notes.txt
This command will output the names of the files starting with draft_, followed by any character, followed by .txt:
$ echo draft_?.txt
draft_1.txt draft_2.txt
This command will output the names of the files starting with the letter b or c:
$ echo [bc]*
backup.sql candidates.csv
This command will output the names of the files starting with draft_, followed by any number ranging from 0 to 9, followed by any other character:
$ echo draft_[0-9]*
draft_1.txt draft_2.txt
The brace expansion
The brace expansion {} is used to generate arbitrary strings or sequences from a pattern contained in braces.
In its general form it is composed of the pattern itself eventually surrounded by an optional prefix and/or suffix string:
[prefix]{pattern}[suffix]
Where {pattern} is either:
- A comma-separated list of strings without spaces:
{string1,string2,...,stringN} - A range of integers or single characters:
{<start>..<end>}
Example
This command will output a sequence of numbers:
$ echo {1..5}
1 2 3 4 5
This command will output a sequence of letters in reverse order:
$ echo {e..a}
e d c b a
This command will output a list of files with the same name but different file extensions:
$ echo photo.{gif,jpeg,png}
photo.gif photo.jpeg photo.png
This command will output a list of files with the same base name combined with different numbers and file extensions:
$ echo photo_{2023,2024}.{jpeg,png}
photo_2023.jpeg photo_2023.png photo_2024.jpeg photo_2024.png
Summary
Here's a summary of what you've learned in this lesson:
- The
~expansion is a shortcut to the home directory. - The
?expansion will match any single character. - The
*expansion will match zero or more characters. - The
[]expansion will match any single character within the specified set or range. - The
{}expansion is used to generate arbitrary strings or sequences from a pattern.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery