Print this page
4023 - Typo in file(1) manpage and various others
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/man/man1/printf.1
+++ new/usr/src/man/man1/printf.1
1 1 '\" te
2 2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
3 3 .\" Copyright 1992, X/Open Company Limited All Rights Reserved
4 4 .\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
5 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 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 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 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 9 .\" This notice shall appear on any product containing this material.
10 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 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 12 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 13 .TH PRINTF 1 "Aug 11, 2009"
14 14 .SH NAME
15 15 printf \- write formatted output
16 16 .SH SYNOPSIS
17 17 .SS "/usr/bin/printf"
18 18 .LP
19 19 .nf
20 20 \fBprintf\fR \fIformat\fR [\fIargument\fR]...
21 21 .fi
22 22
23 23 .SS "ksh93"
24 24 .LP
25 25 .nf
26 26 \fBprintf\fR \fIformat\fR [\fIstring\fR...]
27 27 .fi
28 28
29 29 .SH DESCRIPTION
30 30 .SS "/usr/bin/printf"
31 31 .sp
32 32 .LP
33 33 The \fBprintf\fR utility writes each string operand to standard output using
34 34 \fIformat\fR to control the output format.
35 35 .SH OPERANDS
36 36 .SS "/usr/bin/printf"
37 37 .sp
38 38 .LP
39 39 The following operands are supported by \fB/usr/bin/printf\fR:
40 40 .sp
41 41 .ne 2
42 42 .na
43 43 \fB\fIformat\fR\fR
44 44 .ad
45 45 .RS 12n
46 46 A string describing the format to use to write the remaining operands. The
47 47 \fIformat\fR operand is used as the \fIformat\fR string described on the
48 48 \fBformats\fR(5) manual page, with the following exceptions:
49 49 .RS +4
50 50 .TP
51 51 .ie t \(bu
52 52 .el o
53 53 A \fBSPACE\fR character in the format string, in any context other than a flag
54 54 of a conversion specification, is treated as an ordinary character that is
55 55 copied to the output.
56 56 .RE
57 57 .RS +4
58 58 .TP
59 59 .ie t \(bu
60 60 .el o
61 61 A character in the format string is treated as a character, not as a
62 62 \fBSPACE\fR character.
63 63 .RE
64 64 .RS +4
65 65 .TP
66 66 .ie t \(bu
67 67 .el o
68 68 In addition to the escape sequences described on the \fBformats\fR(5) manual
69 69 page (\fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR,
70 70 \fB\et\fR, \fB\ev\fR), \fB\e\fR\fIddd\fR, where \fIddd\fR is a one-, two- or
71 71 three-digit octal number, is written as a byte with the numeric value specified
72 72 by the octal number.
73 73 .RE
74 74 .RS +4
75 75 .TP
76 76 .ie t \(bu
77 77 .el o
78 78 The program does not precede or follow output from the \fBd\fR or \fBu\fR
79 79 conversion specifications with blank characters not specified by the
80 80 \fIformat\fR operand.
81 81 .RE
82 82 .RS +4
83 83 .TP
84 84 .ie t \(bu
85 85 .el o
86 86 The program does not precede output from the \fBo\fR conversion specification
87 87 with zeros not specified by the \fIformat\fR operand.
88 88 .RE
89 89 .RS +4
90 90 .TP
91 91 .ie t \(bu
92 92 .el o
93 93 An additional conversion character, \fBb\fR, is supported as follows. The
94 94 argument is taken to be a string that can contain backslash-escape sequences.
95 95 The following backslash-escape sequences are supported:
96 96 .RS +4
97 97 .TP
98 98 .ie t \(bu
99 99 .el o
100 100 the escape sequences listed on the \fBformats\fR(5) manual page (\fB\e\e\fR,
101 101 \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR),
102 102 which are converted to the characters they represent
103 103 .RE
104 104 .RS +4
105 105 .TP
106 106 .ie t \(bu
107 107 .el o
108 108 \fB\e0\fR\fIddd\fR, where \fIddd\fR is a zero-, one-, two- or three-digit octal
109 109 number that is converted to a byte with the numeric value specified by the
110 110 octal number
111 111 .RE
112 112 .RS +4
113 113 .TP
114 114 .ie t \(bu
115 115 .el o
116 116 \fB\ec\fR, which is written and causes \fBprintf\fR to ignore any remaining
117 117 characters in the string operand containing it, any remaining string operands
118 118 and any additional characters in the \fIformat\fR operand.
119 119 .RE
120 120 .RE
121 121 The interpretation of a backslash followed by any other sequence of characters
122 122 is unspecified.
123 123 .sp
124 124 Bytes from the converted string are written until the end of the string or the
125 125 number of bytes indicated by the precision specification is reached. If the
126 126 precision is omitted, it is taken to be infinite, so all bytes up to the end of
127 127 the converted string are written. For each specification that consumes an
128 128 argument, the next argument operand is evaluated and converted to the
129 129 appropriate type for the conversion as specified below. The \fIformat\fR
130 130 operand is reused as often as necessary to satisfy the argument operands. Any
131 131 extra \fBc\fR or \fBs\fR conversion specifications are evaluated as if a null
132 132 string argument were supplied; other extra conversion specifications are
133 133 evaluated as if a zero argument were supplied. If the \fIformat\fR operand
134 134 contains no conversion specifications and \fIargument\fR operands are present,
135 135 the results are unspecified. If a character sequence in the \fIformat\fR
136 136 operand begins with a \fB%\fR character, but does not form a valid conversion
137 137 specification, the behavior is unspecified.
138 138 .RE
139 139
140 140 .sp
141 141 .ne 2
142 142 .na
143 143 \fB\fIargument\fR\fR
144 144 .ad
145 145 .RS 12n
146 146 The strings to be written to standard output, under the control of
147 147 \fBformat\fR. The \fIargument\fR operands are treated as strings if the
148 148 corresponding conversion character is \fBb\fR, \fBc\fR or \fBs\fR. Otherwise,
149 149 it is evaluated as a C constant, as described by the ISO C standard, with the
150 150 following extensions:
151 151 .RS +4
152 152 .TP
153 153 .ie t \(bu
154 154 .el o
155 155 A leading plus or minus sign is allowed.
156 156 .RE
157 157 .RS +4
158 158 .TP
159 159 .ie t \(bu
160 160 .el o
161 161 If the leading character is a single- or double-quote, the value is the numeric
162 162 value in the underlying codeset of the character following the single- or
163 163 double-quote.
164 164 .RE
165 165 If an argument operand cannot be completely converted into an internal value
166 166 appropriate to the corresponding conversion specification, a diagnostic message
167 167 is written to standard error and the utility does not exit with a zero exit
168 168 status, but continues processing any remaining operands and writes the value
169 169 accumulated at the time the error was detected to standard output.
170 170 .RE
171 171
172 172 .SS "ksh93"
173 173 .sp
174 174 .LP
175 175 The \fIformat\fR operands support the full range of ANSI C/C99/XPG6 formatting
176 176 specifiers as well as additional specifiers:
177 177 .sp
178 178 .ne 2
179 179 .na
180 180 \fB\fB%b\fR\fR
181 181 .ad
182 182 .RS 6n
183 183 Each character in the string operand is processed specially, as follows:
184 184 .sp
185 185 .ne 2
186 186 .na
187 187 \fB\fB\ea\fR\fR
188 188 .ad
189 189 .RS 8n
190 190 Alert character.
191 191 .RE
192 192
193 193 .sp
194 194 .ne 2
195 195 .na
196 196 \fB\fB\eb\fR\fR
197 197 .ad
198 198 .RS 8n
199 199 Backspace character.
200 200 .RE
201 201
202 202 .sp
203 203 .ne 2
204 204 .na
205 205 \fB\fB\ec\fR\fR
206 206 .ad
207 207 .RS 8n
208 208 Terminate output without appending NEWLINE. The remaining string operands are
209 209 ignored.
210 210 .RE
211 211
212 212 .sp
213 213 .ne 2
214 214 .na
215 215 \fB\fB\eE\fR\fR
216 216 .ad
217 217 .RS 8n
218 218 Escape character (\fBASCII\fR octal \fB033\fR).
219 219 .RE
220 220
221 221 .sp
222 222 .ne 2
223 223 .na
224 224 \fB\fB\ef\fR\fR
225 225 .ad
226 226 .RS 8n
227 227 FORM FEED character.
228 228 .RE
229 229
230 230 .sp
231 231 .ne 2
232 232 .na
233 233 \fB\fB\en\fR\fR
234 234 .ad
235 235 .RS 8n
236 236 NEWLINE character.
237 237 .RE
238 238
239 239 .sp
240 240 .ne 2
241 241 .na
242 242 \fB\fB\et\fR\fR
243 243 .ad
244 244 .RS 8n
245 245 TAB character.
246 246 .RE
247 247
248 248 .sp
249 249 .ne 2
250 250 .na
251 251 \fB\fB\ev\fR\fR
252 252 .ad
253 253 .RS 8n
254 254 Vertical tab character.
255 255 .RE
256 256
257 257 .sp
258 258 .ne 2
259 259 .na
260 260 \fB\fB\e\e\fR\fR
261 261 .ad
262 262 .RS 8n
263 263 Backslash character.
264 264 .RE
265 265
266 266 .sp
267 267 .ne 2
268 268 .na
269 269 \fB\fB\e0\fR\fIx\fR\fR
270 270 .ad
271 271 .RS 8n
272 272 The 8-bit character whose \fBASCII\fR code is the \fB1\fR-, \fB2\fR-, or
273 273 \fB3\fR-digit octal number \fIx\fR.
274 274 .RE
275 275
276 276 .RE
277 277
278 278 .sp
279 279 .ne 2
280 280 .na
281 281 \fB\fB%B\fR\fR
282 282 .ad
283 283 .RS 6n
284 284 Treat the argument as a variable name and output the value without converting
285 285 it to a string. This is most useful for variables of type \fB-b\fR.
286 286 .RE
287 287
288 288 .sp
289 289 .ne 2
290 290 .na
291 291 \fB\fB%H\fR\fR
292 292 .ad
293 293 .RS 6n
294 294 Output string with characters \fB<\fR, \fB&\fR, \fB>\fR, \fB"\fR, and
295 295 non-printable characters, properly escaped for use in HTML and XML documents.
296 296 .RE
297 297
298 298 .sp
299 299 .ne 2
300 300 .na
301 301 \fB\fB%P\fR\fR
302 302 .ad
303 303 .RS 6n
304 304 Treat \fIstring\fR as an extended regular expression and convert it to a shell
305 305 pattern.
306 306 .RE
307 307
308 308 .sp
309 309 .ne 2
310 310 .na
311 311 \fB\fB%q\fR\fR
312 312 .ad
313 313 .RS 6n
314 314 Output \fIstring\fR quoted in a manner that it can be read in by the shell to
315 315 get back the same string. However, empty strings resulting from missing string
316 316 operands are not quoted.
317 317 .RE
318 318
319 319 .sp
320 320 .ne 2
321 321 .na
322 322 \fB\fB%R\fR\fR
323 323 .ad
324 324 .RS 6n
325 325 Treat \fIstring\fR as an shell pattern expression and convert it to an extended
326 326 regular expression.
327 327 .RE
328 328
329 329 .sp
330 330 .ne 2
331 331 .na
332 332 \fB\fB%T\fR\fR
333 333 .ad
334 334 .RS 6n
335 335 Treat \fIstring\fR as a date/time string and format it. The \fBT\fR can be
336 336 preceded by (\fIdformat\fR), where \fIdformat\fR is a date format as defined by
337 337 the \fBdate\fR(1) command.
338 338 .RE
339 339
340 340 .sp
341 341 .ne 2
342 342 .na
343 343 \fB\fB%Z\fR\fR
344 344 .ad
345 345 .RS 6n
346 346 Output a byte whose value is \fB0\fR.
347 347 .RE
348 348
349 349 .sp
350 350 .LP
351 351 When performing conversions of \fIstring\fR to satisfy a numeric format
352 352 specifier, if the first character of \fIstring\fR is \fB"or'\fR, the value is
353 353 the numeric value in the underlying code set of the character following the
354 354 \fB"or'\fR. Otherwise, \fIstring\fR is treated like a shell arithmetic
355 355 expression and evaluated.
356 356 .sp
357 357 .LP
358 358 If a \fIstring\fR operand cannot be completely converted into a value
359 359 appropriate for that format specifier, an error occurs, but remaining
360 360 \fIstring\fR operands continue to be processed.
361 361 .sp
362 362 .LP
363 363 In addition to the format specifier extensions, the following extensions of
364 364 ANSI C/C99/XPG6 are permitted in format specifiers:
365 365 .RS +4
366 366 .TP
367 367 .ie t \(bu
368 368 .el o
369 369 The escape sequences \fB\eE\fR and \fB\ee\fR expand to the escape character
370 370 which is octal 033 in ASCII.
371 371 .RE
372 372 .RS +4
373 373 .TP
374 374 .ie t \(bu
375 375 .el o
376 376 The escape sequence \fB\ecx\fR expands to CTRL-x.
377 377 .RE
378 378 .RS +4
379 379 .TP
380 380 .ie t \(bu
381 381 .el o
382 382 The escape sequence \fB\eC[.\fR\fIname\fR\fB\&.]\fR expands to the collating
383 383 element \fIname\fR.
384 384 .RE
385 385 .RS +4
386 386 .TP
387 387 .ie t \(bu
388 388 .el o
389 389 The escape sequence \fB\ex{hex}\fRexpands to the character corresponding to the
390 390 hexadecimal value \fBhex\fR.
391 391 .RE
392 392 .RS +4
393 393 .TP
394 394 .ie t \(bu
395 395 .el o
396 396 The format modifier flag = can be used to center a field to a specified width.
397 397 When the output is a terminal, the character width is used rather than the
398 398 number of bytes.
399 399 .RE
400 400 .RS +4
401 401 .TP
402 402 .ie t \(bu
403 403 .el o
404 404 Each of the integral format specifiers can have a third modifier after width
405 405 and precision that specifies the base of the conversion from 2 to 64. In this
406 406 case, the \fB#\fR modifier causes \fIbase\fR\fB#\fR to be prepended to the
407 407 value.
408 408 .RE
409 409 .RS +4
410 410 .TP
411 411 .ie t \(bu
412 412 .el o
413 413 The \fB#\fR modifier can be used with the \fBd\fR specifier when no base is
414 414 specified to cause the output to be written in units of 1000 with a suffix of
415 415 one of \fBk M G T P E\fR.
416 416 .RE
417 417 .RS +4
418 418 .TP
419 419 .ie t \(bu
420 420 .el o
421 421 The \fB#\fR modifier can be used with the \fBi\fR specifier to cause the output
422 422 to be written in units of \fB1024\fR with a suffix of one of \fBKi Mi Gi Ti Pi
423 423 Ei\fR.
424 424 .RE
425 425 .sp
426 426 .LP
427 427 If there are more \fIstring\fR operands than format specifiers, the format
428 428 string is reprocessed from the beginning. If there are fewer \fIstring\fR
429 429 operands than format specifiers, then \fIstring\fR specifiers are treated as if
430 430 empty strings were supplied, numeric conversions are treated as if \fB0\fR was
431 431 supplied, and time conversions are treated as if \fBnow\fR was supplied.
432 432 .sp
433 433 .LP
434 434 \fB/usr/bin/printf\fR is equivalent to \fBksh93\fR's \fBprintf\fR built-in and
435 435 \fBprint -f\fR, which allows additional options to be specified.
436 436 .SH USAGE
437 437 .SS "/usr/bin/printf"
438 438 .sp
439 439 .LP
440 440 The \fBprintf\fR utility, like the \fBprintf\fR(3C) function on which it is
441 441 based, makes no special provision for dealing with multi-byte characters when
442 442 using the \fB%c\fR conversion specification. Applications should be extremely
443 443 cautious using either of these features when there are multi-byte characters in
444 444 the character set.
445 445 .sp
446 446 .LP
447 447 Field widths and precisions cannot be specified as \fB*\fR.
448 448 .sp
449 449 .LP
450 450 The \fB%b\fR conversion specification is not part of the ISO C standard; it has
451 451 been added here as a portable way to process backslash escapes expanded in
452 452 string operands as provided by the \fBecho\fR utility. See also the USAGE
453 453 section of the \fBecho\fR(1) manual page for ways to use \fBprintf\fR as a
454 454 replacement for all of the traditional versions of the \fBecho\fR utility.
455 455 .sp
456 456 .LP
457 457 If an argument cannot be parsed correctly for the corresponding conversion
458 458 specification, the \fBprintf\fR utility reports an error. Thus, overflow and
459 459 extraneous characters at the end of an argument being used for a numeric
460 460 conversion are to be reported as errors.
461 461 .sp
462 462 .LP
463 463 It is not considered an error if an argument operand is not completely used for
464 464 a \fBc\fR or \fBs\fR conversion or if a string operand's first or second
465 465 character is used to get the numeric value of a character.
466 466 .SH EXAMPLES
467 467 .SS "/usr/bin/printf"
468 468 .LP
469 469 \fBExample 1 \fRPrinting a Series of Prompts
470 470 .sp
471 471 .LP
472 472 The following example alerts the user, then prints and reads a series of
473 473 prompts:
474 474
475 475 .sp
476 476 .in +2
477 477 .nf
478 478 example% \fBprintf "\eaPlease fill in the following: \enName: "
479 479 read name
480 480 printf "Phone number: "
481 481 read phone\fR
482 482 .fi
483 483 .in -2
484 484 .sp
485 485
486 486 .LP
487 487 \fBExample 2 \fRPrinting a Table of Calculations
488 488 .sp
489 489 .LP
490 490 The following example prints a table of calculations. It reads out a list of
491 491 right and wrong answers from a file, calculates the percentage correctly, and
492 492 prints them out. The numbers are right-justified and separated by a single tab
493 493 character. The percentage is written to one decimal place of accuracy:
494 494
495 495 .sp
496 496 .in +2
497 497 .nf
498 498 example% \fBwhile read right wrong ; do
499 499 percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
500 500 printf "%2d right\et%2d wrong\et(%s%%)\en" \e
501 501 $right $wrong $percent
502 502 done < database_file\fR
503 503 .fi
504 504 .in -2
505 505 .sp
506 506
507 507 .LP
508 508 \fBExample 3 \fRPrinting number strings
509 509 .sp
510 510 .LP
511 511 The command:
512 512
513 513 .sp
514 514 .in +2
515 515 .nf
516 516 example% \fBprintf "%5d%4d\en" 1 21 321 4321 54321\fR
517 517 .fi
518 518 .in -2
519 519 .sp
520 520
521 521 .sp
522 522 .LP
523 523 produces:
524 524
525 525 .sp
526 526 .in +2
527 527 .nf
528 528 1 21
529 529 3214321
530 530 54321 0
531 531 .fi
532 532 .in -2
533 533 .sp
534 534
535 535 .sp
536 536 .LP
537 537 The \fIformat\fR operand is used three times to print all of the given strings
538 538 and that a \fB0\fR was supplied by \fBprintf\fR to satisfy the last \fB%4d\fR
539 539 conversion specification.
540 540
541 541 .LP
542 542 \fBExample 4 \fRTabulating Conversion Errors
543 543 .sp
544 544 .LP
545 545 The following example tabulates conversion errors.
546 546
547 547 .sp
548 548 .LP
549 549 The \fBprintf\fR utility tells the user when conversion errors are detected
550 550 while producing numeric output. These results would be expected on an
551 551 implementation with 32-bit twos-complement integers when \fB%d\fR is specified
552 552 as the \fIformat\fR operand:
553 553
554 554 .sp
555 555
556 556 .sp
557 557 .TS
558 558 box;
559 559 c c c
560 560 l l l .
561 561 Arguments Standard Diagnostic
562 562 5a 5 printf: 5a not completely converted
563 563 9999999999 2147483647 printf: 9999999999: Results too large
564 564 -9999999999 -2147483648 printf: -9999999999: Results too large
565 565 ABC 0 printf: ABC expected numeric value
566 566 .TE
567 567
568 568 .sp
569 569 .LP
570 570 The value shown on standard output is what would be expected as the return
571 571 value from the function \fBstrtol\fR(3C). A similar correspondence exists
572 572 between \fB%u\fR and \fBstrtoul\fR(3C), and \fB%e\fR, \fB%f\fR and \fB%g\fR and
573 573 \fBstrtod\fR(3C).
574 574
575 575 .LP
576 576 \fBExample 5 \fRPrinting Output for a Specific Locale
577 577 .sp
578 578 .LP
579 579 The following example prints output for a specific locale. In a locale using
580 580 the ISO/IEC 646:1991 standard as the underlying codeset, the command:
581 581
582 582 .sp
583 583 .in +2
584 584 .nf
585 585 example% \fBprintf "%d\en" 3 +3 -3 \e'3 \e"+3 "'-3"\fR
586 586 .fi
587 587 .in -2
588 588 .sp
589 589
590 590 .sp
591 591 .LP
592 592 produces:
593 593
594 594 .sp
595 595
596 596 .sp
597 597 .TS
598 598 box;
599 599 l l
600 600 l l .
601 601 \fB3\fR Numeric value of constant 3
602 602 \fB3\fR Numeric value of constant 3
603 603 \fB\(mi3\fR Numeric value of constant \(mi3
604 604 \fB51\fR T{
605 605 Numeric value of the character `3' in the ISO/IEC 646:1991 standard codeset
606 606 T}
607 607 \fB43\fR T{
608 608 Numeric value of the character `+' in the ISO/IEC 646:1991 standard codeset
609 609 T}
610 610 \fB45\fR T{
611 611 Numeric value of the character `\(mi' in the SO/IEC 646:1991 standard codeset
612 612 T}
613 613 .TE
614 614
615 615 .sp
616 616 .LP
617 617 In a locale with multi-byte characters, the value of a character is intended to
618 618 be the value of the equivalent of the \fBwchar_t\fR representation of the
619 619 character.
620 620
621 621 .sp
622 622 .LP
623 623 If an argument operand cannot be completely converted into an internal value
624 624 appropriate to the corresponding conversion specification, a diagnostic message
625 625 is written to standard error and the utility does exit with a zero exit status,
626 626 but continues processing any remaining operands and writes the value
627 627 accumulated at the time the error was detected to standard output.
628 628
629 629 .LP
630 630 \fBExample 6 \fRAlternative floating point representation 1
↓ open down ↓ |
630 lines elided |
↑ open up ↑ |
631 631 .sp
632 632 .LP
633 633 The \fBprintf\fR utility supports an alternative floating point representation
634 634 (see \fBprintf\fR(3C) entry for the "\fB%a\fR"/"\fB%A\fR"), which allows the
635 635 output of floating-point values in a format that avoids the usual base16 to
636 636 base10 rounding errors.
637 637
638 638 .sp
639 639 .in +2
640 640 .nf
641 -example% printf "%a\n" 2 3.1 NaN
641 +example% printf "%a\en" 2 3.1 NaN
642 642 .fi
643 643 .in -2
644 644 .sp
645 645
646 646 .sp
647 647 .LP
648 648 produces:
649 649
650 650 .sp
651 651 .in +2
652 652 .nf
653 653 0x1.0000000000000000000000000000p+01
654 654 0x1.8ccccccccccccccccccccccccccdp+01
655 655 nan
656 656 .fi
657 657 .in -2
658 658 .sp
659 659
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
660 660 .LP
661 661 \fBExample 7 \fRAlternative floating point representation 2
662 662 .sp
663 663 .LP
664 664 The following example shows two different representations of the same
665 665 floating-point value.
666 666
667 667 .sp
668 668 .in +2
669 669 .nf
670 -example% x=2 ; printf "%f == %a\n" x x
670 +example% x=2 ; printf "%f == %a\en" x x
671 671 .fi
672 672 .in -2
673 673 .sp
674 674
675 675 .sp
676 676 .LP
677 677 produces:
678 678
679 679 .sp
680 680 .in +2
681 681 .nf
682 682 2.000000 == 0x1.0000000000000000000000000000p+01
683 683 .fi
684 684 .in -2
685 685 .sp
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
686 686
687 687 .LP
688 688 \fBExample 8 \fROutput of unicode values
689 689 .sp
690 690 .LP
691 691 The following command will print the EURO unicode symbol (code-point 0x20ac).
692 692
693 693 .sp
694 694 .in +2
695 695 .nf
696 -example% LC_ALL=en_US.UTF-8 printf "\u[20ac]\n"
696 +example% LC_ALL=en_US.UTF-8 printf "\u[20ac]\en"
697 697 .fi
698 698 .in -2
699 699 .sp
700 700
701 701 .sp
702 702 .LP
703 703 produces:
704 704
705 705 .sp
706 706 .in +2
707 707 .nf
708 708 <euro>
709 709 .fi
710 710 .in -2
711 711 .sp
712 712
713 713 .sp
714 714 .LP
715 715 where "<euro>" represents the EURO currency symbol character.
716 716
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
717 717 .LP
718 718 \fBExample 9 \fRConvert unicode character to unicode code-point value
719 719 .sp
720 720 .LP
721 721 The following command will print the hexadecimal value of a given character.
722 722
723 723 .sp
724 724 .in +2
725 725 .nf
726 726 example% export LC_ALL=en_US.UTF-8
727 -example% printf "%x\n" "'<euro>"
727 +example% printf "%x\en" "'<euro>"
728 728 .fi
729 729 .in -2
730 730 .sp
731 731
732 732 .sp
733 733 .LP
734 734 where "<euro>" represents the EURO currency symbol character (code-point
735 735 0x20ac).
736 736
737 737 .sp
738 738 .LP
739 739 produces:
740 740
741 741 .sp
742 742 .in +2
743 743 .nf
↓ open down ↓ |
6 lines elided |
↑ open up ↑ |
744 744 20ac
745 745 .fi
746 746 .in -2
747 747 .sp
748 748
749 749 .LP
750 750 \fBExample 10 \fRPrint the numeric value of an ASCII character
751 751 .sp
752 752 .in +2
753 753 .nf
754 -example% printf "%d\n" "'A"
754 +example% printf "%d\en" "'A"
755 755 .fi
756 756 .in -2
757 757 .sp
758 758
759 759 .sp
760 760 .LP
761 761 produces:
762 762
763 763 .sp
764 764 .in +2
765 765 .nf
766 766 65
767 767 .fi
768 768 .in -2
769 769 .sp
770 770
771 771 .LP
772 772 \fBExample 11 \fRPrint the language-independent date and time format
773 773 .sp
774 774 .LP
775 775 To print the language-independent date and time format, the following statement
776 776 could be used:
777 777
778 778 .sp
779 779 .in +2
780 780 .nf
781 781 example% printf "format" weekday month day hour min
782 782 .fi
783 783 .in -2
784 784 .sp
785 785
786 786 .sp
787 787 .LP
788 788 For example,
789 789
790 790 .sp
791 791 .in +2
792 792 .nf
793 793 $ printf format "Sunday" "July" 3 10 2
794 794 .fi
↓ open down ↓ |
30 lines elided |
↑ open up ↑ |
795 795 .in -2
796 796 .sp
797 797
798 798 .sp
799 799 .LP
800 800 For American usage, format could be the string:
801 801
802 802 .sp
803 803 .in +2
804 804 .nf
805 -"%s, %s %d, %d:%.2d\n"
805 +"%s, %s %d, %d:%.2d\en"
806 806 .fi
807 807 .in -2
808 808 .sp
809 809
810 810 .sp
811 811 .LP
812 812 producing the message:
813 813
814 814 .sp
815 815 .in +2
816 816 .nf
817 817 Sunday, July 3, 10:02
818 818 .fi
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
819 819 .in -2
820 820 .sp
821 821
822 822 .sp
823 823 .LP
824 824 Whereas for EU usage, format could be the string:
825 825
826 826 .sp
827 827 .in +2
828 828 .nf
829 -"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
829 +"%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
830 830 .fi
831 831 .in -2
832 832 .sp
833 833
834 834 .sp
835 835 .LP
836 836 Note that the '$' characters must be properly escaped, such as
837 837
838 838 .sp
839 839 .in +2
840 840 .nf
841 -"%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\n" in this case
841 +"%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\en" in this case
842 842 .fi
843 843 .in -2
844 844 .sp
845 845
846 846 .sp
847 847 .LP
848 848 producing the message:
849 849
850 850 .sp
851 851 .in +2
852 852 .nf
853 853 Sunday, 3. July, 10:02
854 854 .fi
855 855 .in -2
856 856 .sp
857 857
858 858 .SH ENVIRONMENT VARIABLES
859 859 .sp
860 860 .LP
861 861 See \fBenviron\fR(5) for descriptions of the following environment variables
862 862 that affect the execution of \fBprintf\fR: \fBLANG\fR, \fBLC_ALL\fR,
863 863 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, \fBLC_NUMERIC\fR, and \fBNLSPATH\fR.
864 864 .SH EXIT STATUS
865 865 .sp
866 866 .LP
867 867 The following exit values are returned:
868 868 .sp
869 869 .ne 2
870 870 .na
871 871 \fB\fB0\fR\fR
872 872 .ad
873 873 .RS 6n
874 874 Successful completion.
875 875 .RE
876 876
877 877 .sp
878 878 .ne 2
879 879 .na
880 880 \fB\fB>0\fR\fR
881 881 .ad
882 882 .RS 6n
883 883 An error occurred.
884 884 .RE
885 885
886 886 .SH ATTRIBUTES
887 887 .sp
888 888 .LP
889 889 See \fBattributes\fR(5) for descriptions of the following attributes:
890 890 .SS "/usr/bin/printf"
891 891 .sp
892 892
893 893 .sp
894 894 .TS
895 895 box;
896 896 c | c
897 897 l | l .
898 898 ATTRIBUTE TYPE ATTRIBUTE VALUE
899 899 _
900 900 CSI Enabled
901 901 _
902 902 Interface Stability Committed
903 903 _
904 904 Standard See \fBstandards\fR(5).
905 905 .TE
906 906
907 907 .SS "ksh93"
908 908 .sp
909 909
910 910 .sp
911 911 .TS
912 912 box;
913 913 c | c
914 914 l | l .
915 915 ATTRIBUTE TYPE ATTRIBUTE VALUE
916 916 _
917 917 Interface Stability Uncommitted
918 918 .TE
919 919
920 920 .SH SEE ALSO
921 921 .sp
922 922 .LP
↓ open down ↓ |
71 lines elided |
↑ open up ↑ |
923 923 \fBawk\fR(1), \fBbc\fR(1), \fBdate\fR(1), \fBecho\fR(1), \fBksh93\fR(1),
924 924 \fBprintf\fR(3C), \fBstrtod\fR(3C), \fBstrtol\fR(3C), \fBstrtoul\fR(3C),
925 925 \fBattributes\fR(5), \fBenviron\fR(5), \fBformats\fR(5), \fBstandards\fR(5)
926 926 .SH NOTES
927 927 .sp
928 928 .LP
929 929 Using format specifiers (characters following '%') which are not listed in the
930 930 \fBprintf\fR(3C) or this manual page will result in undefined behavior.
931 931 .sp
932 932 .LP
933 -Using escape sequences (the character following a backslash ('\')) which are
933 +Using escape sequences (the character following a backslash ('\e')) which are
934 934 not listed in the \fBprintf\fR(3C) or this manual page will result in undefined
935 935 behavior.
936 936 .sp
937 937 .LP
938 938 Floating-point values follow C99, XPG6 and IEEE 754 standard behavior and can
939 939 handle values the same way as the platform's |\fBlong double\fR| datatype.
940 940 .sp
941 941 .LP
942 942 Floating-point values handle the sign separately which allows signs for values
943 943 like NaN (for example, -nan), Infinite (for example, -inf) and zero (for
944 944 example, -0.0).
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX