Ten Bash Parameter Expansions You'll Actually Use
Ishe Chinyoka
- 3 minutes readTable of Contents
One of the quiet superpowers of Bash is something many shell users overlook: parameter expansion.
In this post, we explored a major benefit of bash’s parameter expansions. In this nugget, we are going to look at ten useful things you can accomplish with them.
If you’ve ever written a command that pipes a variable through sed, cut, or tr just to remove a filename extension or change a few characters, chances are Bash could have done it for you without invoking another program.
Parameter expansion is Bash’s built-in language for manipulating strings. It is fast, expressive, and once you learn a handful of expansions, you’ll find yourself using them almost every day.
Here are ten that every textsmith should know.
1. Remove a Filename Extension
file="report.pdf"
echo "${file%.*}"Output:
reportThe % operator removes the shortest suffix matching the pattern from the end of the string.
Perfect for stripping file extensions before creating new filenames.
2. Remove the Longest Suffix
archive="backup.tar.gz"
echo "${archive%%.*}"Output:
backupThe double %% removes the longest matching suffix.
This is useful when filenames contain multiple extensions.
3. Remove Everything Before the First Slash
path="/home/ishe/Documents/report.txt"
echo "${path#*/}"Output:
home/ishe/Documents/report.txtThe # operator removes the shortest matching prefix.
4. Extract Just the Filename
path="/home/ishe/Documents/report.txt"
echo "${path##*/}"Output:
report.txtThe double ## removes the longest matching prefix.
This is one of the most common Bash tricks and behaves much like the basename command.
5. Replace the First Match
name="chapter_one_draft"
echo "${name/_/-}"Output:
chapter-one_draftOnly the first occurrence is replaced.
6. Replace Every Match
name="chapter_one_draft"
echo "${name//_/-}"Output:
chapter-one-draftThe double slash replaces all occurrences.
A quick way to normalize filenames without using sed.
7. Find the Length of a String
title="The Textsmith"
echo "${#title}"Output:
13This is invaluable when validating user input or generating reports.
8. Convert to Uppercase
echo "${title^^}"Output:
THE TEXTSMITHUseful when generating identifiers, headings, or environment variables.
9. Convert to Lowercase
title="README"
echo "${title,,}"Output:
readmeThis is especially handy when normalizing filenames or user input.
10. Provide a Default Value
echo "${EDITOR:-vim}"If EDITOR is unset or empty:
vimOtherwise, Bash prints the value already stored in the variable.
This is a common pattern in portable shell scripts because it allows users to override defaults through environment variables.
Quick Reference
| Task | Expansion |
|---|---|
| Remove suffix | ${var%.*} |
| Remove longest suffix | ${var%%.*} |
| Remove prefix | ${var#*/} |
| Remove longest prefix | ${var##*/} |
| Replace first match | ${var/old/new} |
| Replace all matches | ${var//old/new} |
| String length | ${#var} |
| Uppercase | ${var^^} |
| Lowercase | ${var,,} |
| Default value | ${var:-default} |
Why This Matters
One lesson emerges from all of these examples: many shell scripts become simpler when you let Bash manipulate strings itself.
Instead of writing:
filename=$(echo "$file" | sed 's/\.txt$//')you can simply write:
filename="${file%.txt}"Not only is this easier to read, but it also avoids starting another process. While the performance difference is rarely noticeable for a single command, it becomes significant inside loops that process hundreds or thousands of files.
More importantly, parameter expansion encourages a different way of thinking. Rather than seeing Bash merely as a launcher for external utilities, you begin to appreciate it as a small but capable programming language in its own right. External tools such as sed, awk, and grep remain indispensable, but many everyday text transformations never need to leave the shell.
As a textsmith, that’s a useful habit to cultivate: before reaching for another command, ask yourself whether Bash already knows how to do it.