1.\" $NetBSD: printf.1,v 1.27 2018/03/12 09:29:43 wiz Exp $ 2.\" 3.\" Copyright (c) 1989, 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the Institute of Electrical and Electronics Engineers, Inc. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.\" from: @(#)printf.1 8.1 (Berkeley) 6/6/93 34.\" 35.Dd March 11, 2018 36.Dt PRINTF 1 37.Os 38.Sh NAME 39.Nm printf 40.Nd formatted output 41.Sh SYNOPSIS 42.Nm 43.Ar format 44.Op Ar arguments ... 45.Sh DESCRIPTION 46.Nm 47formats and prints its arguments, after the first, under control 48of the 49.Ar format . 50The 51.Ar format 52is a character string which contains three types of objects: plain characters, 53which are simply copied to standard output, character escape sequences which 54are converted and copied to the standard output, and format specifications, 55each of which causes printing of the next successive 56.Ar argument . 57.Pp 58The 59.Ar arguments 60after the first are treated as strings if the corresponding format is 61either 62.Cm b , 63.Cm B , 64.Cm c , 65or 66.Cm s ; 67otherwise it is evaluated as a C constant, with the following extensions: 68.Pp 69.Bl -bullet -offset indent -compact 70.It 71A leading plus or minus sign is allowed. 72.It 73If the leading character is a single or double quote, the value is the ASCII 74code of the next character. 75.El 76.Pp 77The format string is reused as often as necessary to satisfy the 78.Ar arguments . 79Any extra format specifications are evaluated with zero or the null 80string. 81.Pp 82Character escape sequences are in backslash notation as defined in 83.St -ansiC . 84The characters and their meanings are as follows: 85.Bl -tag -width Ds -offset indent 86.It Cm \ee 87Write an 88.Aq escape 89character. 90.It Cm \ea 91Write a 92.Aq bell 93character. 94.It Cm \eb 95Write a 96.Aq backspace 97character. 98.It Cm \ef 99Write a 100.Aq form-feed 101character. 102.It Cm \en 103Write a 104.Aq new-line 105character. 106.It Cm \er 107Write a 108.Aq carriage return 109character. 110.It Cm \et 111Write a 112.Aq tab 113character. 114.It Cm \ev 115Write a 116.Aq vertical tab 117character. 118.It Cm \e\' 119Write a 120.Aq single quote 121character. 122.It Cm \e" 123Write a 124.Aq double quote 125character. 126.It Cm \e\e 127Write a backslash character. 128.It Cm \e Ns Ar num 129Write an 8\-bit character whose ASCII 130value is the 1\-, 2\-, or 3\-digit octal number 131.Ar num . 132.It Cm \ex Ns Ar xx 133Write an 8\-bit character whose ASCII 134value is the 1\- or 2\-digit hexadecimal number 135.Ar xx . 136.El 137.Pp 138Each format specification is introduced by the percent character 139.Pq Dq \&% . 140The remainder of the format specification includes, 141in the following order: 142.Bl -tag -width Ds 143.It Zero or more of the following flags : 144.Bl -tag -width Ds 145.It Cm # 146A 147.Sq # 148character specifying that the value should be printed in an 149.Dq alternative form . 150For 151.Cm b , 152.Cm c , 153.Cm d , 154and 155.Cm s 156formats, this option has no effect. 157For the 158.Cm o 159format the precision of the number is increased to force the first 160character of the output string to a zero. 161For the 162.Cm x 163.Pq Cm X 164format, a non-zero result has the string 165.Li 0x 166.Pq Li 0X 167prepended to it. 168For 169.Cm e , 170.Cm E , 171.Cm f , 172.Cm g , 173and 174.Cm G 175formats, the result will always contain a decimal point, even if no 176digits follow the point (normally, a decimal point only appears in the 177results of those formats if a digit follows the decimal point). 178For 179.Cm g 180and 181.Cm G 182formats, trailing zeros are not removed from the result as they 183would otherwise be. 184.\" I turned this off - decided it isn't a valid use of '#' 185.\" For the 186.\" .Cm B 187.\" format, backslash-escape sequences are expanded first; 188.It Cm \&\- 189A minus sign 190.Sq \- 191which specifies 192.Em left adjustment 193of the output in the indicated field; 194.It Cm \&+ 195A 196.Sq \&+ 197character specifying that there should always be 198a sign placed before the number when using signed formats. 199.It Sq \&\ \& 200A space specifying that a blank should be left before a positive number 201for a signed format. 202A 203.Sq \&+ 204overrides a space if both are used; 205.It Cm \&0 206A zero `0' character indicating that zero-padding should be used 207rather than blank-padding. 208A 209.Sq \- 210overrides a 211.Sq \&0 212if both are used; 213.El 214.It Field Width : 215An optional digit string specifying a 216.Em field width ; 217if the output string has fewer characters than the field width it will 218be blank-padded on the left (or right, if the left-adjustment indicator 219has been given) to make up the field width (note that a leading zero 220is a flag, but an embedded zero is part of a field width); 221.It Precision : 222An optional period, 223.Sq Cm \&. , 224followed by an optional digit string giving a 225.Em precision 226which specifies the number of digits to appear after the decimal point, 227for 228.Cm e 229and 230.Cm f 231formats, or the maximum number of characters to be printed 232from a string 233.Sm off 234.Pf ( Cm b , 235.Sm on 236.Cm B , 237and 238.Cm s 239formats); if the digit string is missing, the precision is treated 240as zero; 241.It Format : 242A character which indicates the type of format to use (one of 243.Cm diouxXfwEgGbBcs ) . 244.El 245.Pp 246A field width or precision may be 247.Sq Cm \&* 248instead of a digit string. 249In this case an 250.Ar argument 251supplies the field width or precision. 252.Pp 253The format characters and their meanings are: 254.Bl -tag -width Fl 255.It Cm diouXx 256The 257.Ar argument 258is printed as a signed decimal (d or i), unsigned octal, unsigned decimal, 259or unsigned hexadecimal (X or x), respectively. 260.It Cm f 261The 262.Ar argument 263is printed in the style 264.Sm off 265.Pf [\-]ddd Cm \&. No ddd 266.Sm on 267where the number of d's 268after the decimal point is equal to the precision specification for 269the argument. 270If the precision is missing, 6 digits are given; if the precision 271is explicitly 0, no digits and no decimal point are printed. 272.It Cm eE 273The 274.Ar argument 275is printed in the style 276.Sm off 277.Pf [\-]d Cm \&. No ddd Cm e No \*(Pmdd 278.Sm on 279where there 280is one digit before the decimal point and the number after is equal to 281the precision specification for the argument; when the precision is 282missing, 6 digits are produced. 283An upper-case E is used for an 284.Sq E 285format. 286.It Cm gG 287The 288.Ar argument 289is printed in style 290.Cm f 291or in style 292.Cm e 293.Pq Cm E 294whichever gives full precision in minimum space. 295.It Cm b 296Characters from the string 297.Ar argument 298are printed with backslash-escape sequences expanded. 299.Pp 300The following additional backslash-escape sequences are supported: 301.Bl -tag -width Ds 302.It Cm \ec 303Causes 304.Nm 305to ignore any remaining characters in the string operand containing it, 306any remaining string operands, and any additional characters in 307the format operand. 308.It Cm \e0 Ns Ar num 309Write an 8\-bit character whose ASCII value is the 1\-, 2\-, or 3103\-digit octal number 311.Ar num . 312.It Cm \e^ Ns Ar c 313Write the control character 314.Ar c . 315Generates characters `\e000' through `\e037`, and `\e177' (from `\e^?'). 316.It Cm \eM\- Ns Ar c 317Write the character 318.Ar c 319with the 8th bit set. 320Generates characters `\e241' through `\e376`. 321.It Cm \eM^ Ns Ar c 322Write the control character 323.Ar c 324with the 8th bit set. 325Generates characters `\e200' through `\e237`, and `\e377' (from `\eM^?'). 326.El 327.It Cm B 328Characters from the string 329.Ar argument 330are printed with unprintable characters backslash-escaped using the 331.Sm off 332.Pf ` Cm \e Ar c No ', 333.Pf ` Cm \e^ Ar c No ', 334.Pf ` Cm \eM\- Ar c No ' 335or 336.Pf ` Cm \eM^ Ar c No ', 337.Sm on 338formats described above. 339.It Cm c 340The first character of 341.Ar argument 342is printed. 343.It Cm s 344Characters from the string 345.Ar argument 346are printed until the end is reached or until the number of characters 347indicated by the precision specification is reached; if the 348precision is omitted, all characters in the string are printed. 349.It Cm \&% 350Print a `%'; no argument is used. 351.El 352.Pp 353In no case does a non-existent or small field width cause truncation of 354a field; padding takes place only if the specified field width exceeds 355the actual width. 356.Pp 357If the first character of 358.Ar format 359is a dash, 360.Ar format 361must be preceded by a word consisting of two dashes (--) to prevent it 362from being interpreted as an option string. 363.Sh EXIT STATUS 364.Ex -std 365.Sh SEE ALSO 366.Xr echo 1 , 367.Xr printf 3 , 368.Xr vis 3 , 369.Xr printf 9 370.Sh STANDARDS 371The 372.Nm 373utility conforms to 374.St -p1003.1-2001 . 375.Pp 376Support for the floating point formats and `*' as a field width and precision 377are optional in POSIX. 378.Pp 379The behaviour of the %B format and the \e', \e", \exxx, \ee and 380\e[M][\-|^]c escape sequences are undefined in POSIX. 381.Sh BUGS 382Since the floating point numbers are translated from ASCII to 383floating-point and then back again, floating-point precision may be lost. 384.Pp 385Hexadecimal character constants are restricted to, and should be specified 386as, two character constants. 387This is contrary to the ISO C standard but 388does guarantee detection of the end of the constant. 389.Sh NOTES 390All formats which treat the 391.Ar argument 392as a number first convert the 393.Ar argument 394from its external representation as a character string 395to an internal numeric representation, and then apply the 396format to the internal numeric representation, producing 397another external character string representation. 398One might expect the 399.Cm \&%c 400format to do likewise, but in fact it does not. 401.Pp 402To convert a string representation of a decimal, octal, or hexadecimal 403number into the corresponding character, two nested 404.Nm 405invocations may be used, in which the inner invocation 406converts the input to an octal string, and the outer 407invocation uses the octal string as part of a format. 408For example, the following command outputs the character whose code 409is 0x0A, which is a newline in ASCII: 410.Pp 411.Dl printf \&"$(printf \&"\e\e%o" \&"0x0A")" 412