brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: printf '%-20s %8d' lines up shell output where echo cannot

Shell
~/tips/tip-shell-printf-aligned-output

printf '%-20s %8s\n' NAME SIZE

Formats text into fixed-width columns in a script.

echo gives you no control over width, and printf does.

printf '%-20s %8s\n' NAME SIZE
printf '%-20s %8d\n' backup.tar 1048576

%-20s pads a string to 20 characters, left-aligned. %8d right-aligns an integer in 8 columns. The format is reused if you give more arguments than it has fields, so printf '%s\n' "${arr[@]}" prints one item per line. %q in bash quotes a string so it can be pasted back into the shell.

Further reading: Bash manual: printf

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link