printf formatteert en toont tekst — betrouwbaarder dan echo voor gebruik in scripts.
printf "Hallo wereld\n"
printf "Naam: %s, leeftijd: %d\n" "Jan" 30
| Code | Betekenis |
|---|---|
%s | String |
%d | Geheel getal |
%f | Kommagetal |
%.2f | Twee decimalen |
%10s | Rechts uitlijnen, breedte 10 |
%-10s | Links uitlijnen, breedte 10 |
%05d | Getal met voorloopnullen |
%x | Hexadecimaal |
| Code | Betekenis |
|---|---|
\n | Nieuwe regel |
\t | Tab |
\\ | Backslash |
\a | Pieptoon |
Tabel opmaken:
printf "%-20s %8s %8s\n" "Bestand" "Grootte" "Datum"
printf "%-20s %8d %8s\n" "rapport.pdf" 1024 "2024-03"
Getal met voorloopnullen:
printf "%05d\n" 42
# 00042
Kommagetallen:
printf "%.2f%%\n" 99.567
# 99.57%
In een script zonder newline:
printf "Bezig... "
sleep 1
printf "Klaar.\n"
Gebruik printf in scripts in plaats van echo — echo gedraagt zich inconsistent met escape-tekens op verschillende systemen en shells.