Trim and Default Variables With Parameter Expansion

Strip extensions or supply fallbacks without calling external tools.

macOSbashshellscriptingLow risk
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

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!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

Last verified: 2026-10-13 · Category: Developer