printf
Format and print data
Overview
Formats and prints data according to a specified FORMAT string, similar to the C printf function. It offers more control over output formatting than `echo` and always interprets backslash escapes.
Syntax
printf FORMAT [ARGUMENT]...Common Options
FORMAT stringA string containing literal characters, backslash escapes (e.g., \n, \t), and format specifiers (e.g., %s, %d, %.2f).
%sFormat specifier for a string.
%d, %iFormat specifier for a signed decimal integer.
%fFormat specifier for a floating-point number (decimal notation).
%x, %XFormat specifier for a hexadecimal integer (lowercase or uppercase).
%cFormat specifier for a single character.
%%Literal percent sign.
Width/PrecisionModifiers like %10s (min width 10), %.2f (2 decimal places), %-10s (left-justify).
Note`printf` reuses the FORMAT string if more ARGUMENTs are provided than specifiers. It does not automatically add a newline; use \n.
Examples
Prints "Name: Alice, Age: 30" followed by a newline.
Formats output in columns: "CPU | 007\nRAM | 045".
Prints "Value: 123.46".
Prints "Hex: 0x10, Octal: 020" (with base prefixes).
Redirects the message to standard error (stderr).
Iterates through array elements, printing each on a new line prefixed by "Var: ".
(Bash specific) Prints current time, e.g., "Current time: 14:30:55".
Prints "The item is 'book'." demonstrating single quote escaping.