Shell
printf
Print formatted text.
printformatshellscriptescape
Additional Notes
printf prints formatted text using a format string and arguments, much like the C printf function. Unlike echo, it does not add a newline unless you ask for one, which makes it predictable in scripts.
The first argument is the format; remaining arguments fill its placeholders in order.
Syntax
printf FORMAT [arguments ...]
Parameters
FORMAT: The format string with literal text and placeholders.arguments: Values substituted into the placeholders.
Common Format Specifiers
%s: String.%d,%i: Signed decimal integer.%u: Unsigned integer.%x,%X: Hexadecimal (lower/upper case).%f: Floating-point number.%c: A single character.%b: String that interprets backslash escapes.%%: A literal percent sign.
Common Escapes
\n: Newline.\t: Tab.\\: Backslash.\": Double quote.
Examples
printf "Hello, %s\n" "$USER"
Greet the current user, ending with a newline.
printf "Zero-padded: %05d\n" 42
Print 00042 with a fixed width.
printf "Hex: %x\n" 255
Print ff in hexadecimal.
printf "Pi: %.2f\n" 3.14159
Print Pi: 3.14 with two decimals.
printf "%s: %s\n" name value
Print a simple key: value line.
printf "%.0f\n" "$((bytes / 1024))"
Print an integer division result without decimals.
Practical Notes
printfdoes not append a newline; add\nwhen you need one.%bexpands\nand\tinside the argument;%sdoes not.- Use
%05dor%8sfor aligned, padded columns in scripts. - It is generally safer than
echowhen the input may start with-or contain backslashes.