printf
Text processing
Usage
printf '[format]' [args]Formats text from a template exactly like C's printf; it adds no newline of its own and reuses the format from the start while arguments remain.
Common flags
| Flag | Meaning |
|---|---|
| %s | Insert a string |
| %d | Insert a whole number |
| %.2f | A number with exactly two decimals |
| %-10s | A string padded to ten columns, left aligned |
| \n | A newline; printf never adds one for you |
Examples
printf '%s\t%d\n' apple 5One tab-separated line.
printf '%.2f\n' 3.14159Rounds to two decimals.
printf '%s\n' a b cThe format repeats, so this prints three lines.
These were built to be piped together. Three joined commands are usually shorter than one that does everything.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does printf do?
Formats text from a template exactly like C's printf; it adds no newline of its own and reuses the format from the start while arguments remain.
Q. How do I type it?
printf '[format]' [args] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man printf. This command sits under Text processing.
Related commands
man page: man printf