$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

Trimming & Substitution with Parameter Expansion

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH3 lines
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.

BASH4 lines
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.

Note

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.

BASH5 lines
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.

BASH3 lines
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.

BASH4 lines
for f in report.txt notes.md; do
  base="${f%.*}"
  echo "$base.bak"
done

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

BASH5 lines
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.

Parameter expansion on one value

Visualizer

Parameter expansion changes the value of a variable without starting a subprocess. Each operator is a small pattern or rule applied to the same string.

1 / 6
path=/var/log/app.tar.gz
expressionresultmeaning
${path}/var/log/app.tar.gzthe value
${#path}19length in characters
${path#*/}var/log/app.tar.gzdrop shortest prefix matching */
${path##*/}app.tar.gzdrop longest prefix matching */ (basename)
${path%.*}/var/log/app.tardrop shortest suffix matching .*
${path%%.*}/var/log/appdrop longest suffix matching .*
${path/log/LOG}/var/LOG/app.tar.gzreplace first match
${path//./_}/var/log/app_tar_gzreplace every match
${path^^}/VAR/LOG/APP.TAR.GZuppercase everything
${path:1:3}varoffset 1, length 3

Start with the value. ${path} gives it back, and ${#path} counts its 19 characters, slashes and dots included.

path=/var/log/app.tar.gz; echo "${#path} ${path##*/} ${path%%.*} ${path//./_} ${path:1:3} ${path^^}"
Terminal lab · 2 steps · real Linux in your browser

Trim paths and replace words

Split a path with trimming forms, then replace text with the slash forms.

01
Split a path
Create paths.sh in /workspace. Set p to /home/user/projects/app.tar.gz, then print four results: the value after the first slash with a single number sign, the file name with a double number sign, the part before the first dot with a double percent sign, and the value without the last extension with a single percent sign: ``bash cat > paths.sh <<'EOF' p=/home/user/projects/app.tar.gz echo "${p#*/}" echo "${p##*/}" echo "${p%%.*}" echo "${p%.*}" EOF ``
Show hint for step 1
The number of signs decides the match length. One sign is the shortest match and two signs are the longest.
02
Replace words in a string
Create replace.sh in /workspace. Set s to one two two. Print four results: replace the first two with 2, replace every two with 2, replace one at the start with 1, and replace two at the end with end: ``bash cat > replace.sh <<'EOF' s="one two two" echo "${s/two/2}" echo "${s//two/2}" echo "${s/#one/1}" echo "${s/%two/end}" EOF ``
Show hint for step 2
A single slash replaces the first match, a double slash replaces all, and a number sign or percent sign after the first slash anchors the match.
Check yourself Question 1 of 4
Concept Check Longest prefix removal recall

Which expansion removes the longest prefix matching */ from the value of p?

Recap & Key Takeaways

Parameter patterns are shell wildcards, not regular expressions, and a single # or % removes the shortest match while ## or %% removes the longest.
${p##*/} gives the file name and ${p%/*} gives the directory, without starting basename or dirname.
${s/pattern/text} replaces the first match, ${s//pattern/text} replaces all, and # or % anchors the match to the start or end.
Quote literal stars and brackets in a pattern, because an unquoted star is a wildcard.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue