logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

printf.1p (16374B)


  1. '\" et
  2. .TH PRINTF "1P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. printf
  12. \(em write formatted output
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. printf \fIformat\fB [\fIargument\fR...\fB]\fR
  17. .fi
  18. .SH DESCRIPTION
  19. The
  20. .IR printf
  21. utility shall write formatted operands to the standard output. The
  22. .IR argument
  23. operands shall be formatted under control of the
  24. .IR format
  25. operand.
  26. .SH OPTIONS
  27. None.
  28. .SH OPERANDS
  29. The following operands shall be supported:
  30. .IP "\fIformat\fR" 10
  31. A string describing the format to use to write the remaining operands.
  32. See the EXTENDED DESCRIPTION section.
  33. .IP "\fIargument\fR" 10
  34. The strings to be written to standard output, under the control of
  35. .IR format .
  36. See the EXTENDED DESCRIPTION section.
  37. .SH STDIN
  38. Not used.
  39. .SH "INPUT FILES"
  40. None.
  41. .SH "ENVIRONMENT VARIABLES"
  42. The following environment variables shall affect the execution of
  43. .IR printf :
  44. .IP "\fILANG\fP" 10
  45. Provide a default value for the internationalization variables that are
  46. unset or null. (See the Base Definitions volume of POSIX.1\(hy2017,
  47. .IR "Section 8.2" ", " "Internationalization Variables"
  48. the precedence of internationalization variables used to determine the
  49. values of locale categories.)
  50. .IP "\fILC_ALL\fP" 10
  51. If set to a non-empty string value, override the values of all the
  52. other internationalization variables.
  53. .IP "\fILC_CTYPE\fP" 10
  54. Determine the locale for the interpretation of sequences of bytes of
  55. text data as characters (for example, single-byte as opposed to
  56. multi-byte characters in arguments).
  57. .IP "\fILC_MESSAGES\fP" 10
  58. .br
  59. Determine the locale that should be used to affect the format and
  60. contents of diagnostic messages written to standard error.
  61. .IP "\fILC_NUMERIC\fP" 10
  62. .br
  63. Determine the locale for numeric formatting. It shall affect the
  64. format of numbers written using the
  65. .BR e ,
  66. .BR E ,
  67. .BR f ,
  68. .BR g ,
  69. and
  70. .BR G
  71. conversion specifier characters (if supported).
  72. .IP "\fINLSPATH\fP" 10
  73. Determine the location of message catalogs for the processing of
  74. .IR LC_MESSAGES .
  75. .SH "ASYNCHRONOUS EVENTS"
  76. Default.
  77. .SH STDOUT
  78. See the EXTENDED DESCRIPTION section.
  79. .SH STDERR
  80. The standard error shall be used only for diagnostic messages.
  81. .SH "OUTPUT FILES"
  82. None.
  83. .SH "EXTENDED DESCRIPTION"
  84. The
  85. .IR format
  86. operand shall be used as the
  87. .IR format
  88. string described in the Base Definitions volume of POSIX.1\(hy2017,
  89. .IR "Chapter 5" ", " "File Format Notation"
  90. with the following exceptions:
  91. .IP " 1." 4
  92. A
  93. <space>
  94. in the format string, in any context other than a flag of a conversion
  95. specification, shall be treated as an ordinary character that is copied
  96. to the output.
  97. .IP " 2." 4
  98. A
  99. .BR ' '
  100. character in the format string shall be treated as a
  101. .BR ' '
  102. character, not as a
  103. <space>.
  104. .IP " 3." 4
  105. In addition to the escape sequences shown in the Base Definitions volume of POSIX.1\(hy2017,
  106. .IR "Chapter 5" ", " "File Format Notation"
  107. (\c
  108. .BR '\e\e' ,
  109. .BR '\ea' ,
  110. .BR '\eb' ,
  111. .BR '\ef' ,
  112. .BR '\en' ,
  113. .BR '\er' ,
  114. .BR '\et' ,
  115. .BR '\ev' ),
  116. .BR \(dq\eddd\(dq ,
  117. where
  118. .IR ddd
  119. is a one, two, or three-digit octal number, shall be written as a byte
  120. with the numeric value specified by the octal number.
  121. .IP " 4." 4
  122. The implementation shall not precede or follow output from the
  123. .BR d
  124. or
  125. .BR u
  126. conversion specifiers with
  127. <blank>
  128. characters not specified by the
  129. .IR format
  130. operand.
  131. .IP " 5." 4
  132. The implementation shall not precede output from the
  133. .BR o
  134. conversion specifier with zeros not specified by the
  135. .IR format
  136. operand.
  137. .IP " 6." 4
  138. The
  139. .BR a ,
  140. .BR A ,
  141. .BR e ,
  142. .BR E ,
  143. .BR f ,
  144. .BR F ,
  145. .BR g ,
  146. and
  147. .BR G
  148. conversion specifiers need not be supported.
  149. .IP " 7." 4
  150. An additional conversion specifier character,
  151. .BR b ,
  152. shall be supported as follows. The argument shall be taken to be a
  153. string that can contain
  154. <backslash>-escape
  155. sequences. The following
  156. <backslash>-escape
  157. sequences shall be supported:
  158. .RS 4
  159. .IP -- 4
  160. The escape sequences listed in the Base Definitions volume of POSIX.1\(hy2017,
  161. .IR "Chapter 5" ", " "File Format Notation"
  162. (\c
  163. .BR '\e\e' ,
  164. .BR '\ea' ,
  165. .BR '\eb' ,
  166. .BR '\ef' ,
  167. .BR '\en' ,
  168. .BR '\er' ,
  169. .BR '\et' ,
  170. .BR '\ev' ),
  171. which shall be converted to the characters they represent
  172. .IP -- 4
  173. .BR \(dq\e0ddd\(dq ,
  174. where
  175. .IR ddd
  176. is a zero, one, two, or three-digit octal number that shall be
  177. converted to a byte with the numeric value specified by the octal
  178. number
  179. .IP -- 4
  180. .BR '\ec' ,
  181. which shall not be written and shall cause
  182. .IR printf
  183. to ignore any remaining characters in the string operand containing it,
  184. any remaining string operands, and any additional characters in the
  185. .IR format
  186. operand
  187. .P
  188. The interpretation of a
  189. <backslash>
  190. followed by any other sequence of characters is unspecified.
  191. .P
  192. Bytes from the converted string shall be written until the end of the
  193. string or the number of bytes indicated by the precision specification
  194. is reached. If the precision is omitted, it shall be taken to be
  195. infinite, so all bytes up to the end of the converted string shall be
  196. written.
  197. .RE
  198. .IP " 8." 4
  199. For each conversion specification that consumes an argument, the next
  200. .IR argument
  201. operand shall be evaluated and converted to the appropriate
  202. type for the conversion as specified below.
  203. .IP " 9." 4
  204. The
  205. .IR format
  206. operand shall be reused as often as necessary to satisfy the
  207. .IR argument
  208. operands. Any extra
  209. .BR b ,
  210. .BR c ,
  211. or
  212. .BR s
  213. conversion specifiers shall be evaluated as if a null string
  214. argument were supplied; other extra conversion specifications shall be
  215. evaluated as if a zero argument were supplied. If the
  216. .IR format
  217. operand contains no conversion specifications and
  218. .IR argument
  219. operands are present, the results are unspecified.
  220. .IP 10. 4
  221. If a character sequence in the
  222. .IR format
  223. operand begins with a
  224. .BR '%'
  225. character, but does not form a valid conversion specification, the
  226. behavior is unspecified.
  227. .IP 11. 4
  228. The argument to the
  229. .BR c
  230. conversion specifier can be a string containing zero or more bytes. If
  231. it contains one or more bytes, the first byte shall be written and any
  232. additional bytes shall be ignored. If the argument is an empty string,
  233. it is unspecified whether nothing is written or a null byte is written.
  234. .P
  235. The
  236. .IR argument
  237. operands shall be treated as strings if the corresponding conversion
  238. specifier is
  239. .BR b ,
  240. .BR c ,
  241. or
  242. .BR s ,
  243. and shall be evaluated as if by the
  244. \fIstrtod\fR()
  245. function if the corresponding conversion specifier is
  246. .BR a ,
  247. .BR A ,
  248. .BR e ,
  249. .BR E ,
  250. .BR f ,
  251. .BR F ,
  252. .BR g ,
  253. or
  254. .BR G .
  255. Otherwise, they shall be evaluated as unsuffixed C integer constants,
  256. as described by the ISO\ C standard, with the following extensions:
  257. .IP " *" 4
  258. A leading
  259. <plus-sign>
  260. or
  261. <hyphen-minus>
  262. shall be allowed.
  263. .IP " *" 4
  264. If the leading character is a single-quote or double-quote, the value
  265. shall be the numeric value in the underlying codeset of the character
  266. following the single-quote or double-quote.
  267. .IP " *" 4
  268. Suffixed integer constants may be allowed.
  269. .P
  270. If an
  271. .IR argument
  272. operand cannot be completely converted into an internal
  273. value appropriate to the corresponding conversion specification, a
  274. diagnostic message shall be written to standard error and the utility
  275. shall not exit with a zero exit status, but shall continue processing
  276. any remaining operands and shall write the value accumulated at the
  277. time the error was detected to standard output.
  278. .P
  279. It shall not be considered an error if an
  280. .IR argument
  281. operand is not completely used for a
  282. .BR b ,
  283. .BR c ,
  284. or
  285. .BR s
  286. conversion.
  287. .SH "EXIT STATUS"
  288. The following exit values shall be returned:
  289. .IP "\00" 6
  290. Successful completion.
  291. .IP >0 6
  292. An error occurred.
  293. .SH "CONSEQUENCES OF ERRORS"
  294. Default.
  295. .LP
  296. .IR "The following sections are informative."
  297. .SH "APPLICATION USAGE"
  298. The floating-point formatting conversion specifications of
  299. \fIprintf\fR()
  300. are not required because all arithmetic in the shell is integer
  301. arithmetic. The
  302. .IR awk
  303. utility performs floating-point calculations and provides its own
  304. .BR printf
  305. function. The
  306. .IR bc
  307. utility can perform arbitrary-precision floating-point arithmetic, but
  308. does not provide extensive formatting capabilities. (This
  309. .IR printf
  310. utility cannot really be used to format
  311. .IR bc
  312. output; it does not support arbitrary precision.) Implementations are
  313. encouraged to support the floating-point conversions as an extension.
  314. .P
  315. Note that this
  316. .IR printf
  317. utility, like the
  318. \fIprintf\fR()
  319. function defined in the System Interfaces volume of POSIX.1\(hy2017 on which it is based, makes no special
  320. provision for dealing with multi-byte characters when using the
  321. .BR %c
  322. conversion specification or when a precision is specified in a
  323. .BR %b
  324. or
  325. .BR %s
  326. conversion specification. Applications should be extremely cautious
  327. using either of these features when there are multi-byte characters in
  328. the character set.
  329. .P
  330. No provision is made in this volume of POSIX.1\(hy2017 which allows field widths and precisions
  331. to be specified as
  332. .BR '*'
  333. since the
  334. .BR '*'
  335. can be replaced directly in the
  336. .IR format
  337. operand using shell variable substitution. Implementations can also
  338. provide this feature as an extension if they so choose.
  339. .P
  340. Hexadecimal character constants as defined in the ISO\ C standard are not
  341. recognized in the
  342. .IR format
  343. operand because there is no consistent way to detect the end of the
  344. constant. Octal character constants are limited to, at most, three
  345. octal digits, but hexadecimal character constants are only terminated
  346. by a non-hex-digit character. In the ISO\ C standard, the
  347. .BR \(dq##\(dq
  348. concatenation operator can be used to terminate a constant and follow
  349. it with a hexadecimal character to be written. In the shell,
  350. concatenation occurs before the
  351. .IR printf
  352. utility has a chance to parse the end of the hexadecimal constant.
  353. .P
  354. The
  355. .BR %b
  356. conversion specification is not part of the ISO\ C standard; it has been added
  357. here as a portable way to process
  358. <backslash>-escapes
  359. expanded in string operands as provided by the
  360. .IR echo
  361. utility. See also the APPLICATION USAGE section of
  362. .IR "\fIecho\fR\^"
  363. for ways to use
  364. .IR printf
  365. as a replacement for all of the traditional versions of the
  366. .IR echo
  367. utility.
  368. .P
  369. If an argument cannot be parsed correctly for the corresponding
  370. conversion specification, the
  371. .IR printf
  372. utility is required to report an error. Thus, overflow and extraneous
  373. characters at the end of an argument being used for a numeric
  374. conversion shall be reported as errors.
  375. .SH EXAMPLES
  376. To alert the user and then print and read a series of prompts:
  377. .sp
  378. .RS 4
  379. .nf
  380. printf "\eaPlease fill in the following: \enName: "
  381. read name
  382. printf "Phone number: "
  383. read phone
  384. .fi
  385. .P
  386. .RE
  387. .P
  388. To read out a list of right and wrong answers from a file, calculate
  389. the percentage correctly, and print them out. The numbers are
  390. right-justified and separated by a single
  391. <tab>.
  392. The percentage is written to one decimal place of accuracy:
  393. .sp
  394. .RS 4
  395. .nf
  396. while read right wrong ; do
  397. percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
  398. printf "%2d right\et%2d wrong\et(%s%%)\en" \e
  399. $right $wrong $percent
  400. done < database_file
  401. .fi
  402. .P
  403. .RE
  404. The command:
  405. .sp
  406. .RS 4
  407. .nf
  408. printf "%5d%4d\en" 1 21 321 4321 54321
  409. .fi
  410. .P
  411. .RE
  412. .P
  413. produces:
  414. .sp
  415. .RS 4
  416. .nf
  417. 1 21
  418. 3214321
  419. 54321 0
  420. .fi
  421. .P
  422. .RE
  423. .P
  424. Note that the
  425. .IR format
  426. operand is used three times to print all of the given strings and that
  427. a
  428. .BR '0'
  429. was supplied by
  430. .IR printf
  431. to satisfy the last
  432. .BR %4d
  433. conversion specification.
  434. .P
  435. The
  436. .IR printf
  437. utility is required to notify the user when conversion errors are
  438. detected while producing numeric output; thus, the following results
  439. would be expected on an implementation with 32-bit twos-complement
  440. integers when
  441. .BR %d
  442. is specified as the
  443. .IR format
  444. operand:
  445. .br
  446. .TS
  447. center tab(@) box;
  448. cB | cB | cB
  449. cB | cB | cB
  450. lf5 | lf5 | lf7.
  451. @Standard
  452. Argument@Output@Diagnostic Output
  453. _
  454. 5a@5@printf: "5a" not completely converted
  455. 9999999999@2147483647@printf: "9999999999" arithmetic overflow
  456. \-9999999999@\-2147483648@printf: "\-9999999999" arithmetic overflow
  457. ABC@0@printf: "ABC" expected numeric value
  458. .TE
  459. .P
  460. The diagnostic message format is not specified, but these examples
  461. convey the type of information that should be reported. Note that the
  462. value shown on standard output is what would be expected as the return
  463. value from the
  464. \fIstrtol\fR()
  465. function as defined in the System Interfaces volume of POSIX.1\(hy2017. A similar correspondence exists
  466. between
  467. .BR %u
  468. and
  469. \fIstrtoul\fR()
  470. and
  471. .BR %e ,
  472. .BR %f ,
  473. and
  474. .BR %g
  475. (if the implementation supports floating-point conversions) and
  476. \fIstrtod\fR().
  477. .P
  478. In a locale using the ISO/IEC\ 646:\|1991 standard as the underlying codeset, the command:
  479. .sp
  480. .RS 4
  481. .nf
  482. printf "%d\en" 3 +3 -3 \e\(aq3 \e"+3 "\(aq-3"
  483. .fi
  484. .P
  485. .RE
  486. .P
  487. produces:
  488. .IP 3 6
  489. Numeric value of constant 3
  490. .IP 3 6
  491. Numeric value of constant 3
  492. .IP "\-3" 6
  493. Numeric value of constant \-3
  494. .IP 51 6
  495. Numeric value of the character
  496. .BR '3'
  497. in the ISO/IEC\ 646:\|1991 standard codeset
  498. .IP 43 6
  499. Numeric value of the character
  500. .BR '\(pl'
  501. in the ISO/IEC\ 646:\|1991 standard codeset
  502. .IP 45 6
  503. Numeric value of the character
  504. .BR '\-'
  505. in the ISO/IEC\ 646:\|1991 standard codeset
  506. .P
  507. Note that in a locale with multi-byte characters, the value of a
  508. character is intended to be the value of the equivalent of the
  509. .BR wchar_t
  510. representation of the character as described in the System Interfaces volume of POSIX.1\(hy2017.
  511. .SH RATIONALE
  512. The
  513. .IR printf
  514. utility was added to provide functionality that has historically been
  515. provided by
  516. .IR echo .
  517. However, due to irreconcilable differences in the various versions of
  518. .IR echo
  519. extant, the version has few special features, leaving those to this new
  520. .IR printf
  521. utility, which is based on one in the Ninth Edition system.
  522. .P
  523. The EXTENDED DESCRIPTION section almost exactly matches the
  524. \fIprintf\fR()
  525. function in the ISO\ C standard, although it is described in terms of the file
  526. format notation in the Base Definitions volume of POSIX.1\(hy2017,
  527. .IR "Chapter 5" ", " "File Format Notation".
  528. .P
  529. Earlier versions of this standard specified that arguments for all
  530. conversions other than
  531. .BR b ,
  532. .BR c ,
  533. and
  534. .BR s
  535. were evaluated in the same way (as C constants, but with stated
  536. exceptions). For implementations supporting the floating-point conversions
  537. it was not clear whether integer conversions need only accept integer
  538. constants and floating-point conversions need only accept floating-point
  539. constants, or whether both types of conversions should accept both
  540. types of constants. Also by not distinguishing between them, the
  541. requirement relating to a leading single-quote or double-quote applied
  542. to floating-point conversions even though this provided no useful
  543. functionality to applications that was not already available through
  544. the integer conversions. The current standard clarifies the situation
  545. by specifying that the arguments for floating-point conversions are
  546. evaluated as if by
  547. \fIstrtod\fR(),
  548. and the arguments for integer conversions are evaluated as C integer
  549. constants, with the special treatment of leading single-quote and
  550. double-quote applying only to integer conversions.
  551. .SH "FUTURE DIRECTIONS"
  552. None.
  553. .SH "SEE ALSO"
  554. .IR "\fIawk\fR\^",
  555. .IR "\fIbc\fR\^",
  556. .IR "\fIecho\fR\^"
  557. .P
  558. The Base Definitions volume of POSIX.1\(hy2017,
  559. .IR "Chapter 5" ", " "File Format Notation",
  560. .IR "Chapter 8" ", " "Environment Variables"
  561. .P
  562. The System Interfaces volume of POSIX.1\(hy2017,
  563. .IR "\fIfprintf\fR\^(\|)",
  564. .IR "\fIstrtod\fR\^(\|)"
  565. .\"
  566. .SH COPYRIGHT
  567. Portions of this text are reprinted and reproduced in electronic form
  568. from IEEE Std 1003.1-2017, Standard for Information Technology
  569. -- Portable Operating System Interface (POSIX), The Open Group Base
  570. Specifications Issue 7, 2018 Edition,
  571. Copyright (C) 2018 by the Institute of
  572. Electrical and Electronics Engineers, Inc and The Open Group.
  573. In the event of any discrepancy between this version and the original IEEE and
  574. The Open Group Standard, the original IEEE and The Open Group Standard
  575. is the referee document. The original Standard can be obtained online at
  576. http://www.opengroup.org/unix/online.html .
  577. .PP
  578. Any typographical or formatting errors that appear
  579. in this page are most likely
  580. to have been introduced during the conversion of the source files to
  581. man page format. To report such errors, see
  582. https://www.kernel.org/doc/man-pages/reporting_bugs.html .