1printf(1) User Commands printf(1)
2
3
4
6 printf - write formatted output
7
9 /usr/bin/printf
10 printf format [argument]...
11
12
13 ksh93
14 printf format [string...]
15
16
18 /usr/bin/printf
19 The printf utility writes each string operand to standard output using
20 format to control the output format.
21
23 /usr/bin/printf
24 The following operands are supported by /usr/bin/printf:
25
26 format A string describing the format to use to write the remain‐
27 ing operands. The format operand is used as the format
28 string described on the formats(5) manual page, with the
29 following exceptions:
30
31 o A SPACE character in the format string, in any
32 context other than a flag of a conversion speci‐
33 fication, is treated as an ordinary character
34 that is copied to the output.
35
36 o A character in the format string is treated as a
37 character, not as a SPACE character.
38
39 o In addition to the escape sequences described on
40 the formats(5) manual page (\\, \a, \b, \f, \n,
41 \r, \t, \v), \ddd, where ddd is a one-, two- or
42 three-digit octal number, is written as a byte
43 with the numeric value specified by the octal
44 number.
45
46 o The program does not precede or follow output
47 from the d or u conversion specifications with
48 blank characters not specified by the format op‐
49 erand.
50
51 o The program does not precede output from the o
52 conversion specification with zeros not speci‐
53 fied by the format operand.
54
55 o An additional conversion character, b, is sup‐
56 ported as follows. The argument is taken to be a
57 string that can contain backslash-escape
58 sequences. The following backslash-escape
59 sequences are supported:
60
61 o the escape sequences listed on the for‐
62 mats(5) manual page (\\, \a, \b, \f, \n, \r,
63 \t, \v), which are converted to the charac‐
64 ters they represent
65
66 o \0ddd, where ddd is a zero-, one-, two- or
67 three-digit octal number that is converted
68 to a byte with the numeric value specified
69 by the octal number
70
71 o \c, which is written and causes printf to
72 ignore any remaining characters in the
73 string operand containing it, any remaining
74 string operands and any additional charac‐
75 ters in the format operand.
76 The interpretation of a backslash followed by any other
77 sequence of characters is unspecified.
78
79 Bytes from the converted string are written until the end
80 of the string or the number of bytes indicated by the pre‐
81 cision specification is reached. If the precision is omit‐
82 ted, it is taken to be infinite, so all bytes up to the end
83 of the converted string are written. For each specification
84 that consumes an argument, the next argument operand is
85 evaluated and converted to the appropriate type for the
86 conversion as specified below. The format operand is reused
87 as often as necessary to satisfy the argument operands. Any
88 extra c or s conversion specifications are evaluated as if
89 a null string argument were supplied; other extra conver‐
90 sion specifications are evaluated as if a zero argument
91 were supplied. If the format operand contains no conversion
92 specifications and argument operands are present, the
93 results are unspecified. If a character sequence in the
94 format operand begins with a % character, but does not form
95 a valid conversion specification, the behavior is unspeci‐
96 fied.
97
98
99 argument The strings to be written to standard output, under the
100 control of format. The argument operands are treated as
101 strings if the corresponding conversion character is b, c
102 or s. Otherwise, it is evaluated as a C constant, as
103 described by the ISO C standard, with the following exten‐
104 sions:
105
106 o A leading plus or minus sign is allowed.
107
108 o If the leading character is a single- or double-
109 quote, the value is the numeric value in the
110 underlying codeset of the character following
111 the single- or double-quote.
112 If an argument operand cannot be completely converted into
113 an internal value appropriate to the corresponding conver‐
114 sion specification, a diagnostic message is written to
115 standard error and the utility does not exit with a zero
116 exit status, but continues processing any remaining oper‐
117 ands and writes the value accumulated at the time the error
118 was detected to standard output.
119
120
121 ksh93
122 The format operands support the full range of ANSI C/C99/XPG6 format‐
123 ting specifiers as well as additional specifiers:
124
125 %b Each character in the string operand is processed specially, as
126 follows:
127
128 \a Alert character.
129
130
131 \b Backspace character.
132
133
134 \c Terminate output without appending NEWLINE. The remaining
135 string operands are ignored.
136
137
138 \E Escape character (ASCII octal 033).
139
140
141 \f FORM FEED character.
142
143
144 \n NEWLINE character.
145
146
147 \t TAB character.
148
149
150 \v Vertical tab character.
151
152
153 \\ Backslash character.
154
155
156 \0x The 8-bit character whose ASCII code is the 1-, 2-, or
157 3-digit octal number x.
158
159
160
161 %B Treat the argument as a variable name and output the value with‐
162 out converting it to a string. This is most useful for variables
163 of type -b.
164
165
166 %H Output string with characters <, &, >, ", and non-printable char‐
167 acters, properly escaped for use in HTML and XML documents.
168
169
170 %P Treat string as an extended regular expression and convert it to
171 a shell pattern.
172
173
174 %q Output string quoted in a manner that it can be read in by the
175 shell to get back the same string. However, empty strings result‐
176 ing from missing string operands are not quoted.
177
178
179 %R Treat string as an shell pattern expression and convert it to an
180 extended regular expression.
181
182
183 %T Treat string as a date/time string and format it. The T can be
184 preceded by (dformat), where dformat is a date format as defined
185 by the date(1) command.
186
187
188 %Z Output a byte whose value is 0.
189
190
191
192 When performing conversions of string to satisfy a numeric format spec‐
193 ifier, if the first character of string is "or', the value is the
194 numeric value in the underlying code set of the character following the
195 "or'. Otherwise, string is treated like a shell arithmetic expression
196 and evaluated.
197
198
199 If a string operand cannot be completely converted into a value appro‐
200 priate for that format specifier, an error occurs, but remaining string
201 operands continue to be processed.
202
203
204 In addition to the format specifier extensions, the following exten‐
205 sions of ANSI C/C99/XPG6 are permitted in format specifiers:
206
207 o The escape sequences \E and \e expand to the escape charac‐
208 ter which is octal 033 in ASCII.
209
210 o The escape sequence \cx expands to CTRL-x.
211
212 o The escape sequence \C[.name.] expands to the collating ele‐
213 ment name.
214
215 o The escape sequence \x{hex}expands to the character corre‐
216 sponding to the hexadecimal value hex.
217
218 o The format modifier flag = can be used to center a field to
219 a specified width. When the output is a terminal, the char‐
220 acter width is used rather than the number of bytes.
221
222 o Each of the integral format specifiers can have a third mod‐
223 ifier after width and precision that specifies the base of
224 the conversion from 2 to 64. In this case, the # modifier
225 causes base# to be prepended to the value.
226
227 o The # modifier can be used with the d specifier when no base
228 is specified to cause the output to be written in units of
229 1000 with a suffix of one of k M G T P E.
230
231 o The # modifier can be used with the i specifier to cause the
232 output to be written in units of 1024 with a suffix of one
233 of Ki Mi Gi Ti Pi Ei.
234
235
236 If there are more string operands than format specifiers, the format
237 string is reprocessed from the beginning. If there are fewer string op‐
238 erands than format specifiers, then string specifiers are treated as if
239 empty strings were supplied, numeric conversions are treated as if 0
240 was supplied, and time conversions are treated as if now was supplied.
241
242
243 /usr/bin/printf is equivalent to ksh93's printf built-in and print -f,
244 which allows additional options to be specified.
245
247 /usr/bin/printf
248 The printf utility, like the printf(3C) function on which it is based,
249 makes no special provision for dealing with multi-byte characters when
250 using the %c conversion specification. Applications should be extremely
251 cautious using either of these features when there are multi-byte char‐
252 acters in the character set.
253
254
255 Field widths and precisions cannot be specified as *.
256
257
258 The %b conversion specification is not part of the ISO C standard; it
259 has been added here as a portable way to process backslash escapes
260 expanded in string operands as provided by the echo utility. See also
261 the USAGE section of the echo(1) manual page for ways to use printf as
262 a replacement for all of the traditional versions of the echo utility.
263
264
265 If an argument cannot be parsed correctly for the corresponding conver‐
266 sion specification, the printf utility reports an error. Thus, overflow
267 and extraneous characters at the end of an argument being used for a
268 numeric conversion are to be reported as errors.
269
270
271 It is not considered an error if an argument operand is not completely
272 used for a c or s conversion or if a string operand's first or second
273 character is used to get the numeric value of a character.
274
276 /usr/bin/printf
277 Example 1 Printing a Series of Prompts
278
279
280 The following example alerts the user, then prints and reads a series
281 of prompts:
282
283
284 example% printf "\aPlease fill in the following: \nName: "
285 read name
286 printf "Phone number: "
287 read phone
288
289
290
291 Example 2 Printing a Table of Calculations
292
293
294 The following example prints a table of calculations. It reads out a
295 list of right and wrong answers from a file, calculates the percentage
296 correctly, and prints them out. The numbers are right-justified and
297 separated by a single tab character. The percentage is written to one
298 decimal place of accuracy:
299
300
301 example% while read right wrong ; do
302 percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
303 printf "%2d right\t%2d wrong\t(%s%%)\n" \
304 $right $wrong $percent
305 done < database_file
306
307
308
309 Example 3 Printing number strings
310
311
312 The command:
313
314
315 example% printf "%5d%4d\n" 1 21 321 4321 54321
316
317
318
319
320 produces:
321
322
323 1 21
324 3214321
325 54321 0
326
327
328
329
330 The format operand is used three times to print all of the given
331 strings and that a 0 was supplied by printf to satisfy the last %4d
332 conversion specification.
333
334
335 Example 4 Tabulating Conversion Errors
336
337
338 The following example tabulates conversion errors.
339
340
341
342 The printf utility tells the user when conversion errors are detected
343 while producing numeric output. These results would be expected on an
344 implementation with 32-bit twos-complement integers when %d is speci‐
345 fied as the format operand:
346
347
348
349
350
351 ┌───────────────────────────────────────────────────────────────────────┐
352 │ Arguments Standard Diagnostic │
353 │5a 5 printf: 5a not completely converted │
354 │9999999999 2147483647 printf: 9999999999: Results too large │
355 │-9999999999 -2147483648 printf: -9999999999: Results too large │
356 │ABC 0 printf: ABC expected numeric value │
357 └───────────────────────────────────────────────────────────────────────┘
358
359
360 The value shown on standard output is what would be expected as the
361 return value from the function strtol(3C). A similar correspondence
362 exists between %u and strtoul(3C), and %e, %f and %g and strtod(3C).
363
364
365 Example 5 Printing Output for a Specific Locale
366
367
368 The following example prints output for a specific locale. In a locale
369 using the ISO/IEC 646:1991 standard as the underlying codeset, the com‐
370 mand:
371
372
373 example% printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"
374
375
376
377
378 produces:
379
380
381
382
383
384 ┌───────────────────────────────────────────────────────────┐
385 │3 Numeric value of constant 3 │
386 │3 Numeric value of constant 3 │
387 │−3 Numeric value of constant −3 │
388 │51 Numeric value of the character `3' in the ISO/IEC │
389 │ 646:1991 standard codeset │
390 │43 Numeric value of the character `+' in the ISO/IEC │
391 │ 646:1991 standard codeset │
392 │45 Numeric value of the character `−' in the SO/IEC │
393 │ 646:1991 standard codeset │
394 └───────────────────────────────────────────────────────────┘
395
396
397 In a locale with multi-byte characters, the value of a character is
398 intended to be the value of the equivalent of the wchar_t representa‐
399 tion of the character.
400
401
402
403 If an argument operand cannot be completely converted into an internal
404 value appropriate to the corresponding conversion specification, a
405 diagnostic message is written to standard error and the utility does
406 exit with a zero exit status, but continues processing any remaining
407 operands and writes the value accumulated at the time the error was
408 detected to standard output.
409
410
411 Example 6 Alternative floating point representation 1
412
413
414 The printf utility supports an alternative floating point representa‐
415 tion (see printf(3C) entry for the "%a"/"%A"), which allows the output
416 of floating-point values in a format that avoids the usual base16 to
417 base10 rounding errors.
418
419
420 example% printf "%a0 2 3.1 NaN
421
422
423
424
425 produces:
426
427
428 0x1.0000000000000000000000000000p+01
429 0x1.8ccccccccccccccccccccccccccdp+01
430 nan
431
432
433
434 Example 7 Alternative floating point representation 2
435
436
437 The following example shows two different representations of the same
438 floating-point value.
439
440
441 example% x=2 ; printf "%f == %a0 x x
442
443
444
445
446 produces:
447
448
449 2.000000 == 0x1.0000000000000000000000000000p+01
450
451
452
453 Example 8 Output of unicode values
454
455
456 The following command will print the EURO unicode symbol (code-point
457 0x20ac).
458
459
460 example% LC_ALL=en_US.UTF-8 printf "[20ac]0
461
462
463
464
465 produces:
466
467
468 <euro>
469
470
471
472
473 where "<euro>" represents the EURO currency symbol character.
474
475
476 Example 9 Convert unicode character to unicode code-point value
477
478
479 The following command will print the hexadecimal value of a given char‐
480 acter.
481
482
483 example% export LC_ALL=en_US.UTF-8
484 example% printf "%x0 "'<euro>"
485
486
487
488
489 where "<euro>" represents the EURO currency symbol character (code-
490 point 0x20ac).
491
492
493
494 produces:
495
496
497 20ac
498
499
500
501 Example 10 Print the numeric value of an ASCII character
502
503 example% printf "%d0 "'A"
504
505
506
507
508 produces:
509
510
511 65
512
513
514
515 Example 11 Print the language-independent date and time format
516
517
518 To print the language-independent date and time format, the following
519 statement could be used:
520
521
522 example% printf "format" weekday month day hour min
523
524
525
526
527 For example,
528
529
530 $ printf format "Sunday" "July" 3 10 2
531
532
533
534
535 For American usage, format could be the string:
536
537
538 "%s, %s %d, %d:%.2d0
539
540
541
542
543 producing the message:
544
545
546 Sunday, July 3, 10:02
547
548
549
550
551 Whereas for EU usage, format could be the string:
552
553
554 "%1$s, %3$d. %2$s, %4$d:%5$.2d0
555
556
557
558
559 Note that the '$' characters must be properly escaped, such as
560
561
562 "%1, %3. %2, %4:%52d0 in this case
563
564
565
566
567 producing the message:
568
569
570 Sunday, 3. July, 10:02
571
572
573
575 See environ(5) for descriptions of the following environment variables
576 that affect the execution of printf: LANG, LC_ALL, LC_CTYPE, LC_MES‐
577 SAGES, LC_NUMERIC, and NLSPATH.
578
580 The following exit values are returned:
581
582 0 Successful completion.
583
584
585 >0 An error occurred.
586
587
589 See attributes(5) for descriptions of the following attributes:
590
591 /usr/bin/printf
592 ┌─────────────────────────────┬─────────────────────────────┐
593 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
594 ├─────────────────────────────┼─────────────────────────────┤
595 │Availability │SUNWloc │
596 ├─────────────────────────────┼─────────────────────────────┤
597 │CSI │Enabled │
598 ├─────────────────────────────┼─────────────────────────────┤
599 │Interface Stability │Committed │
600 ├─────────────────────────────┼─────────────────────────────┤
601 │Standard │See standards(5). │
602 └─────────────────────────────┴─────────────────────────────┘
603
604 ksh93
605 ┌─────────────────────────────┬─────────────────────────────┐
606 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
607 ├─────────────────────────────┼─────────────────────────────┤
608 │Availability │SUNWcsu │
609 ├─────────────────────────────┼─────────────────────────────┤
610 │Interface Stability │Uncommitted │
611 └─────────────────────────────┴─────────────────────────────┘
612
614 awk(1), bc(1), date(1), echo(1), ksh93(1), printf(3C), strtod(3C), str‐
615 tol(3C), strtoul(3C), attributes(5), environ(5), formats(5), stan‐
616 dards(5)
617
619 Using format specifiers (characters following '%') which are not listed
620 in the printf(3C) or this manual page will result in undefined behav‐
621 ior.
622
623
624 Using escape sequences (the character following a backslash ('´)) which
625 are not listed in the printf(3C) or this manual page will result in
626 undefined behavior.
627
628
629 Floating-point values follow C99, XPG6 and IEEE 754 standard behavior
630 and can handle values the same way as the platform's |long double|
631 datatype.
632
633
634 Floating-point values handle the sign separately which allows signs for
635 values like NaN (for example, -nan), Infinite (for example, -inf) and
636 zero (for example, -0.0).
637
638
639
640SunOS 5.11 11 Aug 2009 printf(1)