Skip to content
shellhacks.dev
Bash

Strip every extension from a name

Remove every extension from a name with ${f%%.*} in Bash. The doubled percent sign takes the longest match, where a single one stops at the last dot.

1 min read
Summarize in AI

A file called backup.2026.sql.gz needs to become backup, so removing one suffix at a time is not enough.

Shell
f=backup.2026.sql.gz
printf 'shortest: %s\n' "${f%.*}"
printf 'longest:  %s\n' "${f%%.*}"
Text
shortest: backup.2026.sql
longest:  backup

The percent sign trims from the end of the value. One sign takes the shortest match of the pattern, two take the longest, and with .* the longest match starts at the first dot. The same single versus double rule applies to # and ## at the start of the value, as described in the Bash manual.

Everything after the first dot is gone, so a name that carries a dot in its real stem, such as v1.2-release.tar.gz, comes back as v1. When the stem can hold dots, strip a known list of suffixes instead, one ${f%.gz} at a time.

Share

More snippets

Bash

Strip a file extension

Strip a file extension in Bash with ${f%.*}, which removes only the shortest suffix after the last dot and needs no basename call or subshell.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.