2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
3 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
4 .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
7 .\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
8 .\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\" This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
11 .\" See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with
12 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH PRINTF 1 "Aug 11, 2009"
15 printf \- write formatted output
20 \fBprintf\fR \fIformat\fR [\fIargument\fR]...
26 \fBprintf\fR \fIformat\fR [\fIstring\fR...]
33 The \fBprintf\fR utility writes each string operand to standard output using
34 \fIformat\fR to control the output format.
39 The following operands are supported by \fB/usr/bin/printf\fR:
46 A string describing the format to use to write the remaining operands. The
47 \fIformat\fR operand is used as the \fIformat\fR string described on the
48 \fBformats\fR(5) manual page, with the following exceptions:
53 A \fBSPACE\fR character in the format string, in any context other than a flag
54 of a conversion specification, is treated as an ordinary character that is
61 A character in the format string is treated as a character, not as a
62 \fBSPACE\fR character.
68 In addition to the escape sequences described on the \fBformats\fR(5) manual
69 page (\fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR,
70 \fB\et\fR, \fB\ev\fR), \fB\e\fR\fIddd\fR, where \fIddd\fR is a one-, two- or
71 three-digit octal number, is written as a byte with the numeric value specified
78 The program does not precede or follow output from the \fBd\fR or \fBu\fR
79 conversion specifications with blank characters not specified by the
86 The program does not precede output from the \fBo\fR conversion specification
87 with zeros not specified by the \fIformat\fR operand.
93 An additional conversion character, \fBb\fR, is supported as follows. The
94 argument is taken to be a string that can contain backslash-escape sequences.
95 The following backslash-escape sequences are supported:
100 the escape sequences listed on the \fBformats\fR(5) manual page (\fB\e\e\fR,
101 \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR),
102 which are converted to the characters they represent
108 \fB\e0\fR\fIddd\fR, where \fIddd\fR is a zero-, one-, two- or three-digit octal
109 number that is converted to a byte with the numeric value specified by the
116 \fB\ec\fR, which is written and causes \fBprintf\fR to ignore any remaining
117 characters in the string operand containing it, any remaining string operands
118 and any additional characters in the \fIformat\fR operand.
121 The interpretation of a backslash followed by any other sequence of characters
124 Bytes from the converted string are written until the end of the string or the
125 number of bytes indicated by the precision specification is reached. If the
126 precision is omitted, it is taken to be infinite, so all bytes up to the end of
127 the converted string are written. For each specification that consumes an
128 argument, the next argument operand is evaluated and converted to the
129 appropriate type for the conversion as specified below. The \fIformat\fR
130 operand is reused as often as necessary to satisfy the argument operands. Any
131 extra \fBc\fR or \fBs\fR conversion specifications are evaluated as if a null
132 string argument were supplied; other extra conversion specifications are
133 evaluated as if a zero argument were supplied. If the \fIformat\fR operand
134 contains no conversion specifications and \fIargument\fR operands are present,
135 the results are unspecified. If a character sequence in the \fIformat\fR
136 operand begins with a \fB%\fR character, but does not form a valid conversion
137 specification, the behavior is unspecified.
146 The strings to be written to standard output, under the control of
147 \fBformat\fR. The \fIargument\fR operands are treated as strings if the
148 corresponding conversion character is \fBb\fR, \fBc\fR or \fBs\fR. Otherwise,
149 it is evaluated as a C constant, as described by the ISO C standard, with the
150 following extensions:
155 A leading plus or minus sign is allowed.
161 If the leading character is a single- or double-quote, the value is the numeric
162 value in the underlying codeset of the character following the single- or
165 If an argument operand cannot be completely converted into an internal value
166 appropriate to the corresponding conversion specification, a diagnostic message
167 is written to standard error and the utility does not exit with a zero exit
168 status, but continues processing any remaining operands and writes the value
169 accumulated at the time the error was detected to standard output.
175 The \fIformat\fR operands support the full range of ANSI C/C99/XPG6 formatting
176 specifiers as well as additional specifiers:
183 Each character in the string operand is processed specially, as follows:
208 Terminate output without appending NEWLINE. The remaining string operands are
218 Escape character (\fBASCII\fR octal \fB033\fR).
254 Vertical tab character.
269 \fB\fB\e0\fR\fIx\fR\fR
272 The 8-bit character whose \fBASCII\fR code is the \fB1\fR-, \fB2\fR-, or
273 \fB3\fR-digit octal number \fIx\fR.
284 Treat the argument as a variable name and output the value without converting
285 it to a string. This is most useful for variables of type \fB-b\fR.
294 Output string with characters \fB<\fR, \fB&\fR, \fB>\fR, \fB"\fR, and
295 non-printable characters, properly escaped for use in HTML and XML documents.
304 Treat \fIstring\fR as an extended regular expression and convert it to a shell
314 Output \fIstring\fR quoted in a manner that it can be read in by the shell to
315 get back the same string. However, empty strings resulting from missing string
316 operands are not quoted.
325 Treat \fIstring\fR as an shell pattern expression and convert it to an extended
335 Treat \fIstring\fR as a date/time string and format it. The \fBT\fR can be
336 preceded by (\fIdformat\fR), where \fIdformat\fR is a date format as defined by
337 the \fBdate\fR(1) command.
346 Output a byte whose value is \fB0\fR.
351 When performing conversions of \fIstring\fR to satisfy a numeric format
352 specifier, if the first character of \fIstring\fR is \fB"or'\fR, the value is
353 the numeric value in the underlying code set of the character following the
354 \fB"or'\fR. Otherwise, \fIstring\fR is treated like a shell arithmetic
355 expression and evaluated.
358 If a \fIstring\fR operand cannot be completely converted into a value
359 appropriate for that format specifier, an error occurs, but remaining
360 \fIstring\fR operands continue to be processed.
363 In addition to the format specifier extensions, the following extensions of
364 ANSI C/C99/XPG6 are permitted in format specifiers:
369 The escape sequences \fB\eE\fR and \fB\ee\fR expand to the escape character
370 which is octal 033 in ASCII.
376 The escape sequence \fB\ecx\fR expands to CTRL-x.
382 The escape sequence \fB\eC[.\fR\fIname\fR\fB\&.]\fR expands to the collating
389 The escape sequence \fB\ex{hex}\fRexpands to the character corresponding to the
390 hexadecimal value \fBhex\fR.
396 The format modifier flag = can be used to center a field to a specified width.
397 When the output is a terminal, the character width is used rather than the
404 Each of the integral format specifiers can have a third modifier after width
405 and precision that specifies the base of the conversion from 2 to 64. In this
406 case, the \fB#\fR modifier causes \fIbase\fR\fB#\fR to be prepended to the
413 The \fB#\fR modifier can be used with the \fBd\fR specifier when no base is
414 specified to cause the output to be written in units of 1000 with a suffix of
415 one of \fBk M G T P E\fR.
421 The \fB#\fR modifier can be used with the \fBi\fR specifier to cause the output
422 to be written in units of \fB1024\fR with a suffix of one of \fBKi Mi Gi Ti Pi
427 If there are more \fIstring\fR operands than format specifiers, the format
428 string is reprocessed from the beginning. If there are fewer \fIstring\fR
429 operands than format specifiers, then \fIstring\fR specifiers are treated as if
430 empty strings were supplied, numeric conversions are treated as if \fB0\fR was
431 supplied, and time conversions are treated as if \fBnow\fR was supplied.
434 \fB/usr/bin/printf\fR is equivalent to \fBksh93\fR's \fBprintf\fR built-in and
435 \fBprint -f\fR, which allows additional options to be specified.
437 .SS "/usr/bin/printf"
440 The \fBprintf\fR utility, like the \fBprintf\fR(3C) function on which it is
441 based, makes no special provision for dealing with multi-byte characters when
442 using the \fB%c\fR conversion specification. Applications should be extremely
443 cautious using either of these features when there are multi-byte characters in
447 Field widths and precisions cannot be specified as \fB*\fR.
450 The \fB%b\fR conversion specification is not part of the ISO C standard; it has
451 been added here as a portable way to process backslash escapes expanded in
452 string operands as provided by the \fBecho\fR utility. See also the USAGE
453 section of the \fBecho\fR(1) manual page for ways to use \fBprintf\fR as a
454 replacement for all of the traditional versions of the \fBecho\fR utility.
457 If an argument cannot be parsed correctly for the corresponding conversion
458 specification, the \fBprintf\fR utility reports an error. Thus, overflow and
459 extraneous characters at the end of an argument being used for a numeric
460 conversion are to be reported as errors.
463 It is not considered an error if an argument operand is not completely used for
464 a \fBc\fR or \fBs\fR conversion or if a string operand's first or second
465 character is used to get the numeric value of a character.
467 .SS "/usr/bin/printf"
469 \fBExample 1 \fRPrinting a Series of Prompts
472 The following example alerts the user, then prints and reads a series of
478 example% \fBprintf "\eaPlease fill in the following: \enName: "
480 printf "Phone number: "
487 \fBExample 2 \fRPrinting a Table of Calculations
490 The following example prints a table of calculations. It reads out a list of
491 right and wrong answers from a file, calculates the percentage correctly, and
492 prints them out. The numbers are right-justified and separated by a single tab
493 character. The percentage is written to one decimal place of accuracy:
498 example% \fBwhile read right wrong ; do
499 percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
500 printf "%2d right\et%2d wrong\et(%s%%)\en" \e
501 $right $wrong $percent
502 done < database_file\fR
508 \fBExample 3 \fRPrinting number strings
516 example% \fBprintf "%5d%4d\en" 1 21 321 4321 54321\fR
537 The \fIformat\fR operand is used three times to print all of the given strings
538 and that a \fB0\fR was supplied by \fBprintf\fR to satisfy the last \fB%4d\fR
539 conversion specification.
542 \fBExample 4 \fRTabulating Conversion Errors
545 The following example tabulates conversion errors.
549 The \fBprintf\fR utility tells the user when conversion errors are detected
550 while producing numeric output. These results would be expected on an
551 implementation with 32-bit twos-complement integers when \fB%d\fR is specified
552 as the \fIformat\fR operand:
561 Arguments Standard Diagnostic
562 5a 5 printf: 5a not completely converted
563 9999999999 2147483647 printf: 9999999999: Results too large
564 -9999999999 -2147483648 printf: -9999999999: Results too large
565 ABC 0 printf: ABC expected numeric value
570 The value shown on standard output is what would be expected as the return
571 value from the function \fBstrtol\fR(3C). A similar correspondence exists
572 between \fB%u\fR and \fBstrtoul\fR(3C), and \fB%e\fR, \fB%f\fR and \fB%g\fR and
576 \fBExample 5 \fRPrinting Output for a Specific Locale
579 The following example prints output for a specific locale. In a locale using
580 the ISO/IEC 646:1991 standard as the underlying codeset, the command:
585 example% \fBprintf "%d\en" 3 +3 -3 \e'3 \e"+3 "'-3"\fR
601 \fB3\fR Numeric value of constant 3
602 \fB3\fR Numeric value of constant 3
603 \fB\(mi3\fR Numeric value of constant \(mi3
605 Numeric value of the character `3' in the ISO/IEC 646:1991 standard codeset
608 Numeric value of the character `+' in the ISO/IEC 646:1991 standard codeset
611 Numeric value of the character `\(mi' in the SO/IEC 646:1991 standard codeset
617 In a locale with multi-byte characters, the value of a character is intended to
618 be the value of the equivalent of the \fBwchar_t\fR representation of the
623 If an argument operand cannot be completely converted into an internal value
624 appropriate to the corresponding conversion specification, a diagnostic message
625 is written to standard error and the utility does exit with a zero exit status,
626 but continues processing any remaining operands and writes the value
627 accumulated at the time the error was detected to standard output.
630 \fBExample 6 \fRAlternative floating point representation 1
633 The \fBprintf\fR utility supports an alternative floating point representation
634 (see \fBprintf\fR(3C) entry for the "\fB%a\fR"/"\fB%A\fR"), which allows the
635 output of floating-point values in a format that avoids the usual base16 to
636 base10 rounding errors.
641 example% printf "%a\en" 2 3.1 NaN
653 0x1.0000000000000000000000000000p+01
654 0x1.8ccccccccccccccccccccccccccdp+01
661 \fBExample 7 \fRAlternative floating point representation 2
664 The following example shows two different representations of the same
665 floating-point value.
670 example% x=2 ; printf "%f == %a\en" x x
682 2.000000 == 0x1.0000000000000000000000000000p+01
688 \fBExample 8 \fROutput of unicode values
691 The following command will print the EURO unicode symbol (code-point 0x20ac).
696 example% LC_ALL=en_US.UTF-8 printf "\u[20ac]\en"
715 where "<euro>" represents the EURO currency symbol character.
718 \fBExample 9 \fRConvert unicode character to unicode code-point value
721 The following command will print the hexadecimal value of a given character.
726 example% export LC_ALL=en_US.UTF-8
727 example% printf "%x\en" "'<euro>"
734 where "<euro>" represents the EURO currency symbol character (code-point
750 \fBExample 10 \fRPrint the numeric value of an ASCII character
754 example% printf "%d\en" "'A"
772 \fBExample 11 \fRPrint the language-independent date and time format
775 To print the language-independent date and time format, the following statement
781 example% printf "format" weekday month day hour min
793 $ printf format "Sunday" "July" 3 10 2
800 For American usage, format could be the string:
805 "%s, %s %d, %d:%.2d\en"
812 producing the message:
817 Sunday, July 3, 10:02
824 Whereas for EU usage, format could be the string:
829 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
836 Note that the '$' characters must be properly escaped, such as
841 "%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\en" in this case
848 producing the message:
853 Sunday, 3. July, 10:02
858 .SH ENVIRONMENT VARIABLES
861 See \fBenviron\fR(5) for descriptions of the following environment variables
862 that affect the execution of \fBprintf\fR: \fBLANG\fR, \fBLC_ALL\fR,
863 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, \fBLC_NUMERIC\fR, and \fBNLSPATH\fR.
867 The following exit values are returned:
874 Successful completion.
889 See \fBattributes\fR(5) for descriptions of the following attributes:
890 .SS "/usr/bin/printf"
898 ATTRIBUTE TYPE ATTRIBUTE VALUE
902 Interface Stability Committed
904 Standard See \fBstandards\fR(5).
915 ATTRIBUTE TYPE ATTRIBUTE VALUE
917 Interface Stability Uncommitted
923 \fBawk\fR(1), \fBbc\fR(1), \fBdate\fR(1), \fBecho\fR(1), \fBksh93\fR(1),
924 \fBprintf\fR(3C), \fBstrtod\fR(3C), \fBstrtol\fR(3C), \fBstrtoul\fR(3C),
925 \fBattributes\fR(5), \fBenviron\fR(5), \fBformats\fR(5), \fBstandards\fR(5)
929 Using format specifiers (characters following '%') which are not listed in the
930 \fBprintf\fR(3C) or this manual page will result in undefined behavior.
933 Using escape sequences (the character following a backslash ('\e')) which are
934 not listed in the \fBprintf\fR(3C) or this manual page will result in undefined
938 Floating-point values follow C99, XPG6 and IEEE 754 standard behavior and can
939 handle values the same way as the platform's |\fBlong double\fR| datatype.
942 Floating-point values handle the sign separately which allows signs for values
943 like NaN (for example, -nan), Infinite (for example, -inf) and zero (for