Remove a fixed prefix/suffix from a string in Bash
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Bash, the fastest way to remove a prefix or suffix is parameter expansion. You do not need sed, awk, or cut for simple cases. The shell can do it directly.
The one detail people often miss is that Bash uses patterns, not pure fixed-string functions. That is powerful, but it also means wildcard characters in the prefix or suffix can behave specially unless you account for them.
Basic Prefix and Suffix Removal
For prefixes:
This prints:
For suffixes:
This prints:
The operators are:
- '
#remove from the start' - '
%remove from the end'
Shortest Match Versus Longest Match
Bash also provides greedy forms:
- '
#removes the shortest prefix match' - '
##removes the longest prefix match' - '
%removes the shortest suffix match' - '
%%removes the longest suffix match'
Example:
Output:
The second form removes the longest prefix ending in /, which is why it leaves only the file name.
Similarly for suffixes:
Output:
Removing a Fixed Known Prefix
If the prefix is literally known and simple, parameter expansion is ideal:
That is efficient and easy to read.
You can make it conditional if you only want to remove the prefix when it is present:
This avoids accidental changes when the string does not start with the expected prefix.
Watch Out for Pattern Characters
This is the most important caveat. The text after #, ##, %, or %% is a shell pattern, not a pure fixed string function.
So characters such as *, ?, and [ are special. If your "fixed" prefix or suffix contains those characters, the pattern may match more than you intended.
For ordinary scripting this is not a problem, but if the prefix comes from an external source, be careful. In those cases, an explicit check or another tool may be safer.
Common Real Uses
File path handling:
Extension removal:
URL prefix cleanup:
These are the kinds of jobs where Bash parameter expansion is better than spawning external commands.
Common Pitfalls
The biggest pitfall is forgetting that the removal expression is pattern-based, not a strict fixed-string function. Wildcards in the pattern are meaningful.
Another common mistake is using ## or %% when # or % was intended. That can remove much more of the string than expected.
People also forget to quote the result when printing or reusing it. The parameter expansion itself is fine, but the expanded value may still contain spaces or glob characters afterward.
Finally, do not reach for sed or awk first when Bash can already do the job clearly with one expansion.
Summary
- Use
${var#prefix}to remove a prefix and ${var%suffix}to remove a suffix. - Use
##and%%when you want the longest matching removal. - Bash removal uses shell patterns, not strict fixed-string matching.
- Parameter expansion is usually the cleanest solution for simple prefix or suffix stripping.
- Add an explicit condition when you only want to remove the text if it is definitely present.

