Trim and Default Variables With Parameter Expansion
Strip extensions or supply fallbacks without calling external tools.
Use ${VAR:-default} for a fallback and ${file%%.*} to strip an extension.
01Command
Command
$ file="report.tar.gz"; echo "${file%%.*}"; echo "${FALLBACK:-default}"02What it does
Parameter expansion manipulates variables inside the shell, so stripping suffixes, replacing text and supplying defaults needs no sed or basename call. It is faster and avoids quoting bugs, which is why well-written shell scripts lean on it for argument handling.
03Risk
!Warning: The difference between % and %% (shortest versus longest match) changes the result; test with echo first.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- bash.1.en.html manual pageEvidence: The bash manual page documents shell parameter expansion.
Last verified: 2026-10-13 · Category: Developer