prefix and suffix removal
The ${name#pattern} and ${name%pattern} forms, which remove a matching glob pattern from the start or the end of a value.
In more detail
One # or % removes the shortest match and a doubled ## or %% the longest. The patterns are globs, not regular expressions. ${file%.txt} strips an extension and ${path##*/} keeps only the last part of a path, with no extra process.
Example
file=report.txt; echo "${file%.txt}"Related terms
- parameter expansion — Replacing a dollar sign and a variable name with the variable's value, optionally transformed by an operator inside curly braces.
- glob pattern — A pattern that matches names using * for any run of characters, a question mark for one character and [abc] for one of a set.
- basename — The command that strips the directory part from a path and prints only the last name, as in basename /tmp/work gives work.
Taught in
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Trimming, Replacing And Changing Case — Bash Scripting & Command Line Automation