1 .\" Copyright (c) 1989, 1990, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the Institute of Electrical and Electronics Engineers, Inc.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" @(#)printf.1 8.1 (Berkeley) 6/6/93
42 .Ar format Op Ar arguments ...
46 utility formats and prints its arguments, after the first, under control
51 is a character string which contains three types of objects: plain characters,
52 which are simply copied to standard output, character escape sequences which
53 are converted and copied to the standard output, and format specifications,
54 each of which causes printing of the next successive
59 after the first are treated as strings if the corresponding format is
64 otherwise it is evaluated as a C constant, with the following extensions:
66 .Bl -bullet -offset indent -compact
68 A leading plus or minus sign is allowed.
70 If the leading character is a single or double quote, the value is the
71 character code of the next character.
74 The format string is reused as often as necessary to satisfy the
76 Any extra format specifications are evaluated with zero or the null
79 Character escape sequences are in backslash notation as defined in the
82 The characters and their meanings
85 .Bl -tag -width Ds -offset indent -compact
87 Write a <bell> character.
89 Write a <backspace> character.
91 Write a <form-feed> character.
93 Write a <new-line> character.
95 Write a <carriage return> character.
97 Write a <tab> character.
99 Write a <vertical tab> character.
101 Write a <single quote> character.
103 Write a backslash character.
106 value is the 1-, 2-, or 3-digit
109 Multibyte characters can be constructed using multiple
114 Each format specification is introduced by the percent character
116 The remainder of the format specification includes,
117 in the following order:
119 .It "Zero or more of the following flags:"
123 specifying that the value should be printed in an ``alternate form''.
128 formats, this option has no effect.
131 formats the precision of the number is increased to force the first
132 character of the output string to a zero.
136 format, a non-zero result has the string
141 .Cm a , A , e , E , f , F , g
144 formats, the result will always contain a decimal point, even if no
145 digits follow the point (normally, a decimal point only appears in the
146 results of those formats if a digit follows the decimal point).
151 formats, trailing zeros are not removed from the result as they
154 A minus sign `\-' which specifies
156 of the output in the indicated field;
158 A `+' character specifying that there should always be
159 a sign placed before the number when using signed formats.
161 A space specifying that a blank should be left before a positive number
163 A `+' overrides a space if both are used;
165 A zero `0' character indicating that zero-padding should be used
166 rather than blank-padding.
167 A `\-' overrides a `0' if both are used;
170 An optional digit string specifying a
172 if the output string has fewer bytes than the field width it will
173 be blank-padded on the left (or right, if the left-adjustment indicator
174 has been given) to make up the field width (note that a leading zero
175 is a flag, but an embedded zero is part of a field width);
179 followed by an optional digit string giving a
181 which specifies the number of digits to appear after the decimal point,
186 formats, or the maximum number of bytes to be printed
187 from a string; if the digit string is missing, the precision is treated
190 A character which indicates the type of format to use (one of
191 .Cm diouxXfFeEgGaAcsb ) .
192 The uppercase formats differ from their lowercase counterparts only in
193 that the output of the former is entirely in uppercase.
194 The floating-point format specifiers
196 may be prefixed by an
198 to request that additional precision be used, if available.
201 A field width or precision may be
203 instead of a digit string.
206 supplies the field width or precision.
208 The format characters and their meanings are:
213 is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
214 or unsigned hexadecimal (X or x), respectively.
218 is printed in the style `[\-]ddd.ddd' where the number of d's
219 after the decimal point is equal to the precision specification for
221 If the precision is missing, 6 digits are given; if the precision
222 is explicitly 0, no digits and no decimal point are printed.
223 The values \*[If] and \*[Na] are printed as
231 is printed in the style
234 .Sq Op - Ar d.ddd No \(+- Ar dd
237 is one digit before the decimal point and the number after is equal to
238 the precision specification for the argument; when the precision is
239 missing, 6 digits are produced.
240 The values \*[If] and \*[Na] are printed as
254 whichever gives full precision in minimum space.
260 .Sq Op - Ar h.hhh No \(+- Li p Ar d
262 where there is one digit before the hexadecimal point and the number
263 after is equal to the precision specification for the argument;
264 when the precision is missing, enough digits are produced to convey
265 the argument's exact double-precision floating-point representation.
266 The values \*[If] and \*[Na] are printed as
276 Bytes from the string
278 are printed until the end is reached or until the number of bytes
279 indicated by the precision specification is reached; however if the
280 precision is 0 or missing, the string is printed entirely.
284 but interpret character escapes in backslash notation in the string
286 The permitted escape sequences are slightly different in that
291 and that an additional escape sequence
293 stops further output from this
297 Allows reordering of the output according to
300 Print a `%'; no argument is used.
304 character is defined in the program's locale (category
307 In no case does a non-existent or small field width cause truncation of
308 a field; padding takes place only if the specified field width exceeds
311 Some shells may provide a builtin
313 command which is similar or identical to this utility.
322 .Bd -literal -offset indent
323 $ printf "%s\en" hello
327 Same as above, but notice that the format string is not quoted and hence we
328 do not get the expected behavior:
329 .Bd -literal -offset indent
334 Print arguments forcing sign only for the first argument:
335 .Bd -literal -offset indent
336 $ printf "%+d\en%d\en%d\en" 1 -2 13
342 Same as above, but the single format string will be applied to the three
344 .Bd -literal -offset indent
345 $ printf "%+d\en" 1 -2 13
351 Print number using only two digits after the decimal point:
352 .Bd -literal -offset indent
353 $ printf "%.2f\en" 31.7456
359 behavior of converting arguments of numeric formats not beginning
360 with a digit to the ASCII
361 code of the first character is not supported.
370 command is expected to be compatible with the
379 after the standard library function,
382 ANSI hexadecimal character constants were deliberately not provided.
384 Trying to print a dash ("-") as the first character causes
386 to interpret the dash as a program argument.
391 If the locale contains multibyte characters
399 formats with a precision
400 may not operate as expected.
402 Since the floating point numbers are translated from ASCII
403 to floating-point and then back again, floating-point precision may be lost.
404 (By default, the number is translated to an IEEE-754 double-precision
405 value before being printed.
408 modifier may produce additional precision, depending on the hardware platform.)
410 The escape sequence \e000 is the string terminator.
411 When present in the argument for the
413 format, the argument will be truncated at the \e000 character.
415 Multibyte characters are not recognized in format strings (this is only
418 can appear inside a multibyte character).