Name

printf

Synopsis

    printf formats [strings]

Print strings using the specified formats. formats can be ordinary text characters, C-language escape characters, printf (3S) format conversion specifiers, or, more commonly, a set of conversion arguments listed next.

Tip

printf is built into Bash and ksh93 (see Chapter 4); this entry describes the external version in /usr/bin/printf.

Common Arguments

%b

Process a string argument for backslash escapes (not in printf (3S)). See the description of allowed escapes under echo.

%s

Print the next string.

%[-]m[.n]s

Print the next string, using a field that is m characters wide. Optionally limit the field to print only the first n characters of string. Strings are right-adjusted unless the left-adjustment flag - is specified.

Solaris Argument

% n $s

Print the nth string.

Examples

$ printf '%s %s\n' "My files are in" $HOME
    My files are in /home/arnold
    $ printf '%-25.15s %s\n' "My files are in" $HOME
    My files are in           /home/arnold

Get Unix in a Nutshell, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.