Shell patterns, not regular expressions
The patterns in parameter expansion use the same wildcards as file name matching. A star matches any run of characters, a question mark matches one character, and brackets match a character class. They do not use regular expression syntax, so a dot is an ordinary character and a plus sign has no special meaning. Keep that in mind when you compare these forms with sed, which does use regular expressions.
Removing a prefix with # and ##
A single number sign removes the shortest match of the pattern from the start of the value. Two number signs remove the longest match. The difference shows up only when the pattern could match more than one way, and the star is the usual reason. The pattern */ matches everything up to and including a slash, so the short form removes up to the first slash and the long form removes up to the last one.
p=/home/user/projects/app.tar.gz
echo "${p#*/}"
echo "${p##*/}"The first line prints home/user/projects/app.tar.gz, because only the first slash was removed. The second line prints app.tar.gz, the file name, because the long form removed everything up to the last slash. The second form is the shell equivalent of basename and works without starting a process.
Removing a suffix with % and %%
The percent forms do the same thing at the end of the value. A single percent removes the shortest matching suffix, and a double percent removes the longest. For a file name with several dots, these two produce different answers, and choosing the right one is a common task.
f=archive.tar.gz
echo "${f%.gz}"
echo "${f%%.*}"
echo "${f%.*}"The output is archive.tar, then archive, then archive.tar. The first form removes only the last extension. The second removes from the first dot, which is what you want when you need the base name before any extensions. The third removes the shortest suffix that starts with a dot, which gives the same result as the first form here.
When a name contains no dot, ${f%.*} returns the name unchanged. That is the correct behavior, and it means the expansion is safe to use on any file name.
Replacing text with /
The slash form replaces the first match of a pattern, and the double slash form replaces every match. Anchored forms start with a hash to match at the beginning, or a percent to match at the end. The replacement is a plain string, not a pattern, and an empty replacement deletes the match.
s="one two two"
echo "${s/two/2}"
echo "${s//two/2}"
echo "${s/#one/1}"
echo "${s/%two/end}"The output is one 2 two, one 2 2, 1 two two, and one two end. The first line changes only the first match. The second changes all of them. The third anchors to the start, and the fourth anchors to the end.
A common mistake: globs in a pattern
Because the patterns are shell patterns, a value with a special character can change what matches. If you want to remove a literal star or bracket, quote the part of the pattern that must match exactly. Quoting inside the expansion makes those characters literal.
t="price: 5*2"
echo "${t/\*/ times }"
echo "${t/"*"/ times }"The first line uses a backslash to make the star literal, and the second uses quotes for the same purpose. Both print price: 5 times 2. Without the quoting, the star would match any text and the result would be surprising.
Working on a list of names
These forms work inside loops. A common pattern is to derive an output name from each input name, which needs one trim and one suffix change per file. Write the expansion in quotes so that names with spaces survive.
for f in report.txt notes.md; do
base="${f%.*}"
echo "$base.bak"
doneThe output is report.bak and then notes.bak. The loop never starts a process for the trim, so it stays quick even for thousands of names. The same loop with sed would need one process per name.
Hands-on: split a path
Run the block below and predict its four lines. Then change the value to a path without an extension and see which lines change. The lab for this unit asks you to write this as a script and compare the output to an expected text.
p=/var/log/app/server.log.1
echo "dir=${p%/*}"
echo "file=${p##*/}"
echo "stem=${p%%.*}"
echo "last=${p##*.}"The lines are dir=/var/log/app, file=server.log.1, stem=/var/log/app/server, and last=1. Each line removes a different part of the same value.