printf
ShellFundamentals In simulator In real LinuxPrint formatted text
Name: printf = print formatted
You can practise it right away in the simulator, and use it in real Linux mode too.
Description
printf prints text according to a format string, much like C's printf: %s strings, %d integers, %f decimals, with optional width and precision. It adds no newline; write \n yourself.
It behaves more consistently across systems than echo, so scripts use it when output format matters.
Syntax
printf FORMAT [ARGUMENT]...Common options
| %s | String |
| %d | Integer |
| %5.2f | Float: width 5, 2 decimals |
| %-10s | Left-aligned, 10 characters wide (right-aligned without -) |
| %x / %o | An integer in hexadecimal / octal |
| %% | A % sign itself |
| \n | Newline (printf doesn't add one) |
| -v VAR | Store the result in VAR instead of printing it (bash builtin) |