]> git.saurik.com Git - apple/shell_cmds.git/blame - printf/printf.1
shell_cmds-203.tar.gz
[apple/shell_cmds.git] / printf / printf.1
CommitLineData
44bd5ea7
A
1.\" Copyright (c) 1989, 1990, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
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.
44bd5ea7
A
15.\" 4. 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.
18.\"
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
29.\" SUCH DAMAGE.
30.\"
ddb4a88b 31.\" @(#)printf.1 8.1 (Berkeley) 6/6/93
deb63bfb 32.\" $FreeBSD: head/usr.bin/printf/printf.1 264743 2014-04-21 22:47:18Z pfg $
44bd5ea7 33.\"
71aad674 34.Dd April 21, 2014
44bd5ea7
A
35.Dt PRINTF 1
36.Os
37.Sh NAME
38.Nm printf
39.Nd formatted output
40.Sh SYNOPSIS
41.Nm
ddb4a88b 42.Ar format Op Ar arguments ...
44bd5ea7 43.Sh DESCRIPTION
e1a085ba 44The
44bd5ea7 45.Nm
e1a085ba 46utility formats and prints its arguments, after the first, under control
44bd5ea7 47of the
ddb4a88b 48.Ar format .
44bd5ea7
A
49The
50.Ar format
51is a character string which contains three types of objects: plain characters,
52which are simply copied to standard output, character escape sequences which
53are converted and copied to the standard output, and format specifications,
54each of which causes printing of the next successive
ddb4a88b 55.Ar argument .
44bd5ea7
A
56.Pp
57The
58.Ar arguments
59after the first are treated as strings if the corresponding format is
60either
ddb4a88b 61.Cm c , b
44bd5ea7
A
62or
63.Cm s ;
64otherwise it is evaluated as a C constant, with the following extensions:
65.Pp
66.Bl -bullet -offset indent -compact
67.It
68A leading plus or minus sign is allowed.
69.It
ddb4a88b 70If the leading character is a single or double quote, the value is the
71aad674 71character code of the next character.
44bd5ea7
A
72.El
73.Pp
74The format string is reused as often as necessary to satisfy the
ddb4a88b 75.Ar arguments .
44bd5ea7
A
76Any extra format specifications are evaluated with zero or the null
77string.
78.Pp
ddb4a88b
A
79Character escape sequences are in backslash notation as defined in the
80.St -ansiC ,
81with extensions.
44bd5ea7
A
82The characters and their meanings
83are as follows:
ddb4a88b
A
84.Pp
85.Bl -tag -width Ds -offset indent -compact
44bd5ea7
A
86.It Cm \ea
87Write a <bell> character.
88.It Cm \eb
89Write a <backspace> character.
ddb4a88b
A
90.It Cm \ec
91Ignore remaining characters in this string.
44bd5ea7
A
92.It Cm \ef
93Write a <form-feed> character.
94.It Cm \en
95Write a <new-line> character.
96.It Cm \er
97Write a <carriage return> character.
98.It Cm \et
99Write a <tab> character.
100.It Cm \ev
101Write a <vertical tab> character.
102.It Cm \e\'
103Write a <single quote> character.
104.It Cm \e\e
105Write a backslash character.
ddb4a88b 106.It Cm \e Ns Ar num
71aad674 107Write a byte whose
44bd5ea7
A
108value is the 1-, 2-, or 3-digit
109octal number
110.Ar num .
71aad674
A
111Multibyte characters can be constructed using multiple
112.Cm \e Ns Ar num
113sequences.
44bd5ea7
A
114.El
115.Pp
116Each format specification is introduced by the percent character
117(``%'').
118The remainder of the format specification includes,
119in the following order:
120.Bl -tag -width Ds
121.It "Zero or more of the following flags:"
122.Bl -tag -width Ds
123.It Cm #
124A `#' character
ddb4a88b 125specifying that the value should be printed in an ``alternate form''.
44bd5ea7 126For
71aad674 127.Cm b , c , d , s
44bd5ea7 128and
71aad674 129.Cm u
ddb4a88b
A
130formats, this option has no effect.
131For the
44bd5ea7
A
132.Cm o
133formats the precision of the number is increased to force the first
ddb4a88b
A
134character of the output string to a zero.
135For the
44bd5ea7
A
136.Cm x
137.Pq Cm X
138format, a non-zero result has the string
139.Li 0x
140.Pq Li 0X
ddb4a88b
A
141prepended to it.
142For
71aad674 143.Cm a , A , e , E , f , F , g
44bd5ea7 144and
71aad674 145.Cm G
44bd5ea7
A
146formats, the result will always contain a decimal point, even if no
147digits follow the point (normally, a decimal point only appears in the
ddb4a88b
A
148results of those formats if a digit follows the decimal point).
149For
44bd5ea7
A
150.Cm g
151and
152.Cm G
153formats, trailing zeros are not removed from the result as they
154would otherwise be;
155.It Cm \&\-
156A minus sign `\-' which specifies
157.Em left adjustment
158of the output in the indicated field;
159.It Cm \&+
160A `+' character specifying that there should always be
161a sign placed before the number when using signed formats.
162.It Sq \&\ \&
163A space specifying that a blank should be left before a positive number
ddb4a88b
A
164for a signed format.
165A `+' overrides a space if both are used;
44bd5ea7
A
166.It Cm \&0
167A zero `0' character indicating that zero-padding should be used
ddb4a88b
A
168rather than blank-padding.
169A `\-' overrides a `0' if both are used;
44bd5ea7
A
170.El
171.It "Field Width:"
172An optional digit string specifying a
173.Em field width ;
71aad674 174if the output string has fewer bytes than the field width it will
44bd5ea7
A
175be blank-padded on the left (or right, if the left-adjustment indicator
176has been given) to make up the field width (note that a leading zero
177is a flag, but an embedded zero is part of a field width);
178.It Precision:
179An optional period,
180.Sq Cm \&.\& ,
181followed by an optional digit string giving a
182.Em precision
183which specifies the number of digits to appear after the decimal point,
184for
185.Cm e
ddb4a88b 186and
44bd5ea7 187.Cm f
71aad674 188formats, or the maximum number of bytes to be printed
44bd5ea7
A
189from a string; if the digit string is missing, the precision is treated
190as zero;
191.It Format:
192A character which indicates the type of format to use (one of
ddb4a88b
A
193.Cm diouxXfFeEgGaAcsb ) .
194The uppercase formats differ from their lowercase counterparts only in
195that the output of the former is entirely in uppercase.
196The floating-point format specifiers
197.Pq Cm fFeEgGaA
198may be prefixed by an
199.Cm L
200to request that additional precision be used, if available.
44bd5ea7
A
201.El
202.Pp
203A field width or precision may be
204.Sq Cm \&*
205instead of a digit string.
206In this case an
207.Ar argument
208supplies the field width or precision.
209.Pp
210The format characters and their meanings are:
211.Bl -tag -width Fl
212.It Cm diouXx
213The
214.Ar argument
215is printed as a signed decimal (d or i), unsigned octal, unsigned decimal,
216or unsigned hexadecimal (X or x), respectively.
ddb4a88b 217.It Cm fF
44bd5ea7
A
218The
219.Ar argument
ddb4a88b 220is printed in the style `[\-]ddd.ddd' where the number of d's
44bd5ea7
A
221after the decimal point is equal to the precision specification for
222the argument.
223If the precision is missing, 6 digits are given; if the precision
224is explicitly 0, no digits and no decimal point are printed.
ddb4a88b
A
225The values \*[If] and \*[Na] are printed as
226.Ql inf
227and
228.Ql nan ,
229respectively.
44bd5ea7
A
230.It Cm eE
231The
232.Ar argument
ddb4a88b
A
233is printed in the style
234.Cm e
44bd5ea7 235.Sm off
ddb4a88b 236.Sq Op - Ar d.ddd No \(+- Ar dd
44bd5ea7
A
237.Sm on
238where there
239is one digit before the decimal point and the number after is equal to
240the precision specification for the argument; when the precision is
241missing, 6 digits are produced.
ddb4a88b
A
242The values \*[If] and \*[Na] are printed as
243.Ql inf
244and
245.Ql nan ,
246respectively.
44bd5ea7
A
247.It Cm gG
248The
249.Ar argument
250is printed in style
251.Cm f
ddb4a88b 252.Pq Cm F
44bd5ea7
A
253or in style
254.Cm e
255.Pq Cm E
256whichever gives full precision in minimum space.
ddb4a88b
A
257.It Cm aA
258The
44bd5ea7 259.Ar argument
ddb4a88b
A
260is printed in style
261.Sm off
262.Sq Op - Ar h.hhh No \(+- Li p Ar d
263.Sm on
264where there is one digit before the hexadecimal point and the number
265after is equal to the precision specification for the argument;
266when the precision is missing, enough digits are produced to convey
267the argument's exact double-precision floating-point representation.
268The values \*[If] and \*[Na] are printed as
269.Ql inf
270and
271.Ql nan ,
272respectively.
44bd5ea7 273.It Cm c
71aad674 274The first byte of
44bd5ea7
A
275.Ar argument
276is printed.
277.It Cm s
71aad674 278Bytes from the string
44bd5ea7 279.Ar argument
71aad674 280are printed until the end is reached or until the number of bytes
44bd5ea7 281indicated by the precision specification is reached; however if the
71aad674 282precision is 0 or missing, the string is printed entirely.
ddb4a88b
A
283.It Cm b
284As for
285.Cm s ,
286but interpret character escapes in backslash notation in the string
287.Ar argument .
71aad674
A
288The permitted escape sequences are slightly different in that
289octal escapes are
290.Cm \e0 Ns Ar num
291instead of
292.Cm \e Ns Ar num .
293.It Cm n$
294Allows reordering of the output according to
295.Ar argument .
44bd5ea7
A
296.It Cm \&%
297Print a `%'; no argument is used.
298.El
299.Pp
ddb4a88b
A
300The decimal point
301character is defined in the program's locale (category
302.Dv LC_NUMERIC ) .
303.Pp
44bd5ea7
A
304In no case does a non-existent or small field width cause truncation of
305a field; padding takes place only if the specified field width exceeds
306the actual width.
71aad674
A
307.Pp
308Some shells may provide a builtin
309.Nm
310command which is similar or identical to this utility.
311Consult the
312.Xr builtin 1
313manual page.
ddb4a88b
A
314.Sh EXIT STATUS
315.Ex -std
316.Sh COMPATIBILITY
317The traditional
318.Bx
319behavior of converting arguments of numeric formats not beginning
320with a digit to the
321.Tn ASCII
322code of the first character is not supported.
44bd5ea7 323.Sh SEE ALSO
71aad674 324.Xr builtin 1 ,
44bd5ea7 325.Xr echo 1 ,
71aad674 326.Xr sh 1 ,
44bd5ea7
A
327.Xr printf 3
328.Sh STANDARDS
329The
330.Nm
71aad674 331command is expected to be compatible with the
ddb4a88b
A
332.St -p1003.2
333specification.
334.Sh HISTORY
335The
336.Nm
337command appeared in
338.Bx 4.3 Reno .
339It is modeled
340after the standard library function,
341.Xr printf 3 .
71aad674
A
342.Sh CAVEATS
343.Tn ANSI
344hexadecimal character constants were deliberately not provided.
345.Pp
346Trying to print a dash ("-") as the first character causes
347.Nm
348to interpret the dash as a program argument.
349.Nm --
350must be used before
351.Ar format .
352.Pp
353If the locale contains multibyte characters
354(such as UTF-8),
355the
356.Cm c
357format and
358.Cm b
359and
360.Cm s
361formats with a precision
362may not operate as expected.
44bd5ea7
A
363.Sh BUGS
364Since the floating point numbers are translated from
365.Tn ASCII
366to floating-point and
367then back again, floating-point precision may be lost.
ddb4a88b
A
368(By default, the number is translated to an IEEE-754 double-precision
369value before being printed.
370The
371.Cm L
372modifier may produce additional precision, depending on the hardware platform.)
373.Pp
ddb4a88b
A
374The escape sequence \e000 is the string terminator.
375When present in the argument for the
376.Cm b
377format, the argument will be truncated at the \e000 character.
378.Pp
379Multibyte characters are not recognized in format strings (this is only
380a problem if
381.Ql %
382can appear inside a multibyte character).