Name

printf

Synopsis

printf(format [, expr-list ]) [ dest-expr ]

An alternative output statement borrowed from the C language. It has the ability to produce formatted output. It can also be used to output data without automatically producing a newline. format is a string of format specifications and constants. expr-list is a list of arguments corresponding to format specifiers. As for print, output may be redirected to a file or pipe. See the section "printf Formats,” later in the chapter, for a description of allowed format specifiers.

Like any string, format can also contain embedded escape sequences: \n (newline) or \t (tab) being the most common. Spaces and literal text can be placed in the format argument by quoting the entire argument. If there are multiple expressions to be printed, there should be multiple format specifiers.

Example

Using the script:

    { printf("The sum on line %d is %.0f.\n", NR, $1+$2) }

The following input line:

    5   5

produces this output, followed by a newline:

    The sum on line 1 is 10.

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.