home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    


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

An alternative output statement borrowed from the C language. It can produce formatted output and also 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. See print for a description of dest-expr .

format follows the conventions of the C-language printf (3S) library function. Here are a few of the most common formats:


A string.


A decimal number.

% n . m f

A floating-point number; n = total number of digits. m = number of digits after decimal point.

%[-] nc

n specifies minimum field length for format type c , while - left-justifies value in field; otherwise, value is right-justified.

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 formats specified.


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.

Previous: Reference: print UNIX in a Nutshell: System V Edition Next: Reference: rand
Reference: print Book Index Reference: rand

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System