printf
In this page:
printf
printf formats output using format specifiers like %s (string), %d (integer), and %.2f (decimal), similar to C's printf. Unlike echo, it does not add a trailing newline automatically — you must include \n yourself. It gives much more precise control over output formatting than echo.
Note: Use printf instead of echo whenever you need aligned columns or fixed decimal places.
Example: printf
#!/bin/bash
printf "Name: %s, Age: %d\n" "Alice" 30
printf "Pi is approximately %.2f\n" 3.14159
printf "%-10s|%5s\n" "left" "right"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: