Naar de inhoud
← Alle commando's

printf

28-10-2026 door Bernard in Geen categorie

printf formatteert en toont tekst — betrouwbaarder dan echo voor gebruik in scripts.

Basisgebruik

printf "Hallo wereld\n"
printf "Naam: %s, leeftijd: %d\n" "Jan" 30

Opmaakcodes

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

Escape-tekens

Code Betekenis
\n Nieuwe regel
\t Tab
\\ Backslash
\a Pieptoon

Voorbeelden

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"

Tip

Gebruik printf in scripts in plaats van echo — echo gedraagt zich inconsistent met escape-tekens op verschillende systemen en shells.

Reactie? Stuur ons een bericht via ons contactformulier.