1PRINTF(1P)                 POSIX Programmer's Manual                PRINTF(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       printf — write formatted output
14

SYNOPSIS

16       printf format [argument...]
17

DESCRIPTION

19       The printf utility shall write formatted operands to the standard  out‐
20       put. The argument operands shall be formatted under control of the for‐
21       mat operand.
22

OPTIONS

24       None.
25

OPERANDS

27       The following operands shall be supported:
28
29       format    A string describing the format to use to write the  remaining
30                 operands.  See the EXTENDED DESCRIPTION section.
31
32       argument  The  strings to be written to standard output, under the con‐
33                 trol of format.  See the EXTENDED DESCRIPTION section.
34

STDIN

36       Not used.
37

INPUT FILES

39       None.
40

ENVIRONMENT VARIABLES

42       The following environment  variables  shall  affect  the  execution  of
43       printf:
44
45       LANG      Provide  a  default  value for the internationalization vari‐
46                 ables that are unset or null. (See the Base Definitions  vol‐
47                 ume  of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
48                 ables the precedence of internationalization  variables  used
49                 to determine the values of locale categories.)
50
51       LC_ALL    If  set  to  a non-empty string value, override the values of
52                 all the other internationalization variables.
53
54       LC_CTYPE  Determine the locale for the interpretation of  sequences  of
55                 bytes of text data as characters (for example, single-byte as
56                 opposed to multi-byte characters in arguments).
57
58       LC_MESSAGES
59                 Determine the locale that should be used to affect the format
60                 and  contents  of  diagnostic  messages  written  to standard
61                 error.
62
63       LC_NUMERIC
64                 Determine the locale for numeric formatting. It shall  affect
65                 the  format  of  numbers  written using the e, E, f, g, and G
66                 conversion specifier characters (if supported).
67
68       NLSPATH   Determine the location of message catalogs for the processing
69                 of LC_MESSAGES.
70

ASYNCHRONOUS EVENTS

72       Default.
73

STDOUT

75       See the EXTENDED DESCRIPTION section.
76

STDERR

78       The standard error shall be used only for diagnostic messages.
79

OUTPUT FILES

81       None.
82

EXTENDED DESCRIPTION

84       The  format operand shall be used as the format string described in the
85       Base Definitions volume of POSIX.1‐2008, Chapter 5, File  Format  Nota‐
86       tion with the following exceptions:
87
88        1. A <space> in the format string, in any context other than a flag of
89           a conversion specification, shall be treated as an ordinary charac‐
90           ter that is copied to the output.
91
92        2. A  '' character in the format string shall be treated as a '' char‐
93           acter, not as a <space>.
94
95        3. In addition to the escape sequences shown in the  Base  Definitions
96           volume  of  POSIX.1‐2008,  Chapter  5,  File Format Notation ('\\',
97           '\a', '\b', '\f', '\n', '\r', '\t', '\v'), "\ddd", where ddd  is  a
98           one,  two,  or three-digit octal number, shall be written as a byte
99           with the numeric value specified by the octal number.
100
101        4. The implementation shall not precede or follow output from the d or
102           u  conversion  specifiers  with <blank> characters not specified by
103           the format operand.
104
105        5. The implementation shall not precede output from the  o  conversion
106           specifier with zeros not specified by the format operand.
107
108        6. The  a,  A,  e, E, f, F, g, and G conversion specifiers need not be
109           supported.
110
111        7. An additional conversion specifier character, b, shall be supported
112           as  follows.  The  argument  shall be taken to be a string that may
113           contain  <backslash>-escape   sequences.   The   following   <back‐
114           slash>-escape sequences shall be supported:
115
116           --  The  escape  sequences listed in the Base Definitions volume of
117               POSIX.1‐2008, Chapter 5,  File  Format  Notation  ('\\',  '\a',
118               '\b',  '\f',  '\n', '\r', '\t', '\v'), which shall be converted
119               to the characters they represent
120
121           --  "\0ddd", where ddd is a zero, one, two,  or  three-digit  octal
122               number that shall be converted to a byte with the numeric value
123               specified by the octal number
124
125           --  '\c', which shall not be written  and  shall  cause  printf  to
126               ignore  any remaining characters in the string operand contain‐
127               ing it, any remaining string operands, and any additional char‐
128               acters in the format operand
129
130           The  interpretation of a <backslash> followed by any other sequence
131           of characters is unspecified.
132
133           Bytes from the converted string shall be written until the  end  of
134           the string or the number of bytes indicated by the precision speci‐
135           fication is reached. If the precision is omitted, it shall be taken
136           to  be infinite, so all bytes up to the end of the converted string
137           shall be written.
138
139        8. For each conversion specification that consumes  an  argument,  the
140           next  argument  operand  shall  be  evaluated  and converted to the
141           appropriate type for the conversion as specified below.
142
143        9. The format operand shall be reused as often as necessary to satisfy
144           the argument operands. Any extra c or s conversion specifiers shall
145           be evaluated as if a null  string  argument  were  supplied;  other
146           extra  conversion  specifications  shall  be evaluated as if a zero
147           argument were supplied. If the format operand contains  no  conver‐
148           sion  specifications and argument operands are present, the results
149           are unspecified.
150
151       10. If a character sequence in the format operand  begins  with  a  '%'
152           character,  but does not form a valid conversion specification, the
153           behavior is unspecified.
154
155       11. The argument to the c conversion specifier can be a string contain‐
156           ing zero or more bytes. If it contains one or more bytes, the first
157           byte shall be written and any additional bytes shall be ignored. If
158           the  argument is an empty string, it is unspecified whether nothing
159           is written or a null byte is written.
160
161       The argument operands shall be treated as strings if the  corresponding
162       conversion specifier is b, c, or s, and shall be evaluated as if by the
163       strtod() function if the corresponding conversion specifier is a, A, e,
164       E,  f,  F, g, or G.  Otherwise, they shall be evaluated as unsuffixed C
165       integer constants, as described by the ISO C standard, with the follow‐
166       ing extensions:
167
168        *  A leading <plus-sign> or minus-sign shall be allowed.
169
170        *  If  the  leading  character  is a single-quote or double-quote, the
171           value shall be the numeric value in the underlying codeset  of  the
172           character following the single-quote or double-quote.
173
174        *  Suffixed integer constants may be allowed.
175
176       If  an argument operand cannot be completely converted into an internal
177       value appropriate to  the  corresponding  conversion  specification,  a
178       diagnostic  message  shall be written to standard error and the utility
179       shall not exit with a zero exit status, but shall  continue  processing
180       any  remaining  operands  and  shall write the value accumulated at the
181       time the error was detected to standard output.
182
183       It is not considered an error if an argument operand is not  completely
184       used for a c or s conversion.
185

EXIT STATUS

187       The following exit values shall be returned:
188
189        0    Successful completion.
190
191       >0    An error occurred.
192

CONSEQUENCES OF ERRORS

194       Default.
195
196       The following sections are informative.
197

APPLICATION USAGE

199       The floating-point formatting conversion specifications of printf() are
200       not required because all arithmetic in the shell is integer arithmetic.
201       The  awk  utility performs floating-point calculations and provides its
202       own printf function. The bc  utility  can  perform  arbitrary-precision
203       floating-point  arithmetic,  but  does not provide extensive formatting
204       capabilities. (This printf utility cannot really be used to  format  bc
205       output;  it  does not support arbitrary precision.) Implementations are
206       encouraged to support the floating-point conversions as an extension.
207
208       Note that this printf utility, like the printf()  function  defined  in
209       the  System  Interfaces  volume  of  POSIX.1‐2008 on which it is based,
210       makes no special provision for dealing with multi-byte characters  when
211       using  the %c conversion specification or when a precision is specified
212       in  a  %b  or  %s  conversion  specification.  Applications  should  be
213       extremely cautious using either of these features when there are multi-
214       byte characters in the character set.
215
216       No provision is made in this volume of POSIX.1‐2008 which allows  field
217       widths  and  precisions  to  be  specified  as '*' since the '*' can be
218       replaced directly in the format operand using shell variable  substitu‐
219       tion.  Implementations can also provide this feature as an extension if
220       they so choose.
221
222       Hexadecimal character constants as defined in the  ISO C  standard  are
223       not recognized in the format operand because there is no consistent way
224       to detect the end of the constant. Octal character constants  are  lim‐
225       ited  to,  at  most, three octal digits, but hexadecimal character con‐
226       stants are only terminated by a non-hex-digit character. In  the  ISO C
227       standard,  the  "##"  concatenation operator can be used to terminate a
228       constant and follow it with a hexadecimal character to be  written.  In
229       the  shell, concatenation occurs before the printf utility has a chance
230       to parse the end of the hexadecimal constant.
231
232       The %b conversion specification is not part of the ISO C  standard;  it
233       has  been  added  here as a portable way to process <backslash>-escapes
234       expanded in string operands as provided by the echo utility.  See  also
235       the  APPLICATION  USAGE  section  of  echo  for ways to use printf as a
236       replacement for all of the traditional versions of the echo utility.
237
238       If an argument cannot be parsed correctly for the corresponding conver‐
239       sion  specification, the printf utility is required to report an error.
240       Thus, overflow and extraneous characters at  the  end  of  an  argument
241       being used for a numeric conversion shall be reported as errors.
242

EXAMPLES

244       To alert the user and then print and read a series of prompts:
245
246           printf "\aPlease fill in the following: \nName: "
247           read name
248           printf "Phone number: "
249           read phone
250
251       To  read  out  a list of right and wrong answers from a file, calculate
252       the percentage correctly, and print them out. The  numbers  are  right-
253       justified  and  separated by a single <tab>.  The percentage is written
254       to one decimal place of accuracy:
255
256           while read right wrong ; do
257               percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
258               printf "%2d right\t%2d wrong\t(%s%%)\n" \
259                   $right $wrong $percent
260           done < database_file
261
262       The command:
263
264           printf "%5d%4d\n" 1 21 321 4321 54321
265
266       produces:
267
268               1  21
269             3214321
270           54321   0
271
272       Note that the format operand is used three times to print  all  of  the
273       given strings and that a '0' was supplied by printf to satisfy the last
274       %4d conversion specification.
275
276       The printf utility is required  to  notify  the  user  when  conversion
277       errors are detected while producing numeric output; thus, the following
278       results would be expected on an implementation with 32-bit twos-comple‐
279       ment integers when %d is specified as the format operand:
280
281       ┌────────────┬─────────────┬───────────────────────────────────────────┐
282       │            │  Standard   │                                           │
283Argument   Output    Diagnostic Output             
284       ├────────────┼─────────────┼───────────────────────────────────────────┤
285       │5a          │ 5           │ printf: "5a" not completely converted     │
286       │9999999999  │ 2147483647  │ printf: "9999999999" arithmetic overflow  │
287       │−9999999999 │ −2147483648 │ printf: "−9999999999" arithmetic overflow │
288       │ABC         │ 0           │ printf: "ABC" expected numeric value      │
289       └────────────┴─────────────┴───────────────────────────────────────────┘
290       The diagnostic message format is not specified, but these examples con‐
291       vey the type of information that should  be  reported.  Note  that  the
292       value  shown on standard output is what would be expected as the return
293       value from the strtol() function as defined in  the  System  Interfaces
294       volume  of POSIX.1‐2008. A similar correspondence exists between %u and
295       strtoul() and %e, %f, and %g (if the implementation supports  floating-
296       point conversions) and strtod().
297
298       In a locale using the ISO/IEC 646:1991 standard as the underlying code‐
299       set, the command:
300
301           printf "%d\n" 3 +3 −3 \'3 \"+3 "'−3"
302
303       produces:
304
305       3     Numeric value of constant 3
306
307       3     Numeric value of constant 3
308
309       −3    Numeric value of constant −3
310
311       51    Numeric value of the character '3' in the ISO/IEC 646:1991  stan‐
312             dard codeset
313
314       43    Numeric  value of the character '+' in the ISO/IEC 646:1991 stan‐
315             dard codeset
316
317       45    Numeric value of the character '−' in the ISO/IEC 646:1991  stan‐
318             dard codeset
319
320       Note  that in a locale with multi-byte characters, the value of a char‐
321       acter is intended to be the value of the equivalent of the wchar_t rep‐
322       resentation of the character as described in the System Interfaces vol‐
323       ume of POSIX.1‐2008.
324

RATIONALE

326       The printf utility was added to provide functionality that has histori‐
327       cally  been  provided  by echo.  However, due to irreconcilable differ‐
328       ences in the various versions of echo extant, the version has few  spe‐
329       cial features, leaving those to this new printf utility, which is based
330       on one in the Ninth Edition system.
331
332       The EXTENDED DESCRIPTION section almost exactly  matches  the  printf()
333       function  in  the  ISO C standard, although it is described in terms of
334       the  file  format  notation  in  the   Base   Definitions   volume   of
335       POSIX.1‐2008, Chapter 5, File Format Notation.
336
337       Earlier versions of this standard specified that arguments for all con‐
338       versions other than b, c, and s were evaluated in the same  way  (as  C
339       constants,  but with stated exceptions). For implementations supporting
340       the floating-point conversions it was not clear whether integer conver‐
341       sions need only accept integer constants and floating-point conversions
342       need only accept floating-point constants, or  whether  both  types  of
343       conversions  should accept both types of constants. Also by not distin‐
344       guishing between them, the requirement relating to  a  leading  single-
345       quote or double-quote applied to floating-point conversions even though
346       this provided no useful functionality  to  applications  that  was  not
347       already available through the integer conversions. The current standard
348       clarifies the situation by specifying that the arguments for  floating-
349       point  conversions  are  evaluated as if by strtod(), and the arguments
350       for integer conversions are evaluated as C integer constants, with  the
351       special  treatment  of  leading  single-quote and double-quote applying
352       only to integer conversions.
353

FUTURE DIRECTIONS

355       None.
356

SEE ALSO

358       awk, bc, echo
359
360       The Base Definitions volume of POSIX.1‐2008,  Chapter  5,  File  Format
361       Notation, Chapter 8, Environment Variables
362
363       The System Interfaces volume of POSIX.1‐2008, fprintf(), strtod()
364
366       Portions  of  this text are reprinted and reproduced in electronic form
367       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
368       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
369       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
370       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
371       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
372       event of any discrepancy between this version and the original IEEE and
373       The Open Group Standard, the original IEEE and The Open Group  Standard
374       is  the  referee document. The original Standard can be obtained online
375       at http://www.unix.org/online.html .
376
377       Any typographical or formatting errors that appear  in  this  page  are
378       most likely to have been introduced during the conversion of the source
379       files to man page format. To report such errors,  see  https://www.ker
380       nel.org/doc/man-pages/reporting_bugs.html .
381
382
383
384IEEE/The Open Group                  2013                           PRINTF(1P)
Impressum