Aligned Output With printf

Format columns and numbers precisely from a shell script.

macOSbashshellscriptingLow risk
Run printf "%-10s %5d\n" name 42 to print a left-aligned string and right-aligned number.

01Command

Command
$ printf "%-10s %5d\n" name 42

02What it does

printf gives scripts the same formatting vocabulary as C, so reports and tables can be generated without a templating language. It also avoids the portability and quoting traps of echo, making script output predictable.

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 format string is reused for extra arguments; ensure the count matches or intentionally loop.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

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