1.\" $NetBSD: printf.1,v 1.40 2024/08/06 17:23:01 uwe 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 August 6, 2024 36.Dt PRINTF 1 37.Os 38.Sh NAME 39.Nm printf 40.Nd formatted output 41.Sh SYNOPSIS 42.Nm 43.Op Fl L 44.Ar format 45.Op Ar arguments ... 46.Sh DESCRIPTION 47.Nm 48formats and prints its arguments, after the first, under control 49of the 50.Ar format . 51The 52.Ar format 53is a character string which contains three types of objects: plain characters, 54which are simply copied to standard output, character escape sequences which 55are converted and copied to the standard output, and format specifications, 56each of which causes printing of the next successive 57.Ar argument . 58.Pp 59If the first character of 60.Ar format 61is a dash, 62.Ar format 63must be preceded by a word consisting of two dashes 64.Pq Sq Fl Fl 65to prevent it 66from being interpreted as an option string. 67See 68.Xr getopt 3 . 69.Pp 70The 71.Fl L 72option causes all floating point values resulting from format 73conversions to be printed using 74.Vt long double 75formats, rather than the default 76.Vt double . 77.Pp 78The 79.Ar arguments 80after the first are treated as strings if the corresponding format is 81either 82.Cm b , 83.Cm B , 84.Cm c , 85or 86.Cm s ; 87otherwise it is evaluated as a C\~constant, with the following extensions: 88.Bl -bullet -offset indent 89.It 90A leading plus or minus sign is allowed. 91.It 92If the leading character is a single or double quote, the value is the 93code of the next character. 94No further characters are permitted. 95.El 96.Pp 97The format string is reused as often as necessary to satisfy the 98.Ar arguments . 99Any extra format specifications are evaluated with zero or the null 100string. 101.Pp 102Character escape sequences are in backslash notation as defined in 103.St -ansiC . 104The characters and their meanings are as follows: 105.Bl -tag -offset indent -width Cm 106.It Cm \ea 107Write a 108.Aq bell 109character. 110.It Cm \eb 111Write a 112.Aq backspace 113character. 114.It Cm \ee 115Write an 116.Aq escape 117character. 118.It Cm \ef 119Write a 120.Aq form-feed 121character. 122.It Cm \en 123Write a 124.Aq new-line 125character. 126.It Cm \er 127Write a 128.Aq carriage return 129character. 130.It Cm \et 131Write a 132.Aq tab 133character. 134.It Cm \ev 135Write a 136.Aq vertical tab 137character. 138.It Cm \e\(aq 139Write a 140.Aq single quote 141character. 142.It Cm \e\*q 143Write a 144.Aq double quote 145character. 146.It Cm \e\e 147Write a backslash character. 148.It Cm \e Ns Ar num 149Write an 8-bit character whose ASCII 150value is the 1-, 2-, or 3-digit octal number 151.Ar num . 152.It Cm \ex Ns Ar xx 153Write an 8-bit character whose ASCII 154value is the 1- or 2-digit hexadecimal number 155.Ar xx . 156.El 157.Pp 158Each format specification is introduced by the percent character 159.Pq Ql \&% . 160To produce a literal percent 161.Pq Ql \&% 162in the output, write the percent character twice: 163.Pq Ql \&%% . 164This is not a format conversion. 165The remainder of the format specification includes, 166in the following order: 167.Bl -tag -width 5n 168.It Zero or more of the following flags : 169.Bl -tag -width Cm 170.It Cm # 171A 172.Ql \&# 173character specifying that the value should be printed in an 174.Dq alternative form . 175For 176.Cm b , 177.Cm c , 178.Cm C , 179.Cm d , 180and 181.Cm s 182formats, this option has no effect. 183For the 184.Cm o 185format the precision of the number is increased to force the first 186character of the output string to a zero. 187For the 188.Cm x 189.Pq Cm X 190format, a non-zero result has the string 191.Ql 0x 192.Pq Ql 0X 193prepended to it. 194For 195.Cm e , 196.Cm E , 197.Cm f , 198.Cm F , 199.Cm g , 200and 201.Cm G 202formats, the result will always contain a decimal point, even if no 203digits follow the point (normally, a decimal point only appears in the 204results of those formats if a digit follows the decimal point). 205For 206.Cm g 207and 208.Cm G 209formats, trailing zeros are not removed from the result as they 210would otherwise be. 211.\" I turned this off - decided it isn't a valid use of '#' 212.\" For the 213.\" .Cm B 214.\" format, backslash-escape sequences are expanded first; 215.It Cm \&\- 216A minus sign which specifies 217.Em left adjustment 218of the output in the indicated field; 219.It Cm \&+ 220A plus sign which specifies that there should always be 221a sign placed before the number when using signed formats. 222.It Sq Cm \&\ \& 223A 224.Aq space 225character which specifies that a space should be left before 226a positive number for a signed format. 227A 228.Ql \&+ 229overrides a 230.Aq space 231if both are used; 232.It Cm \&0 233A digit zero character which specifies that zero-padding should be used 234rather than space-padding. 235A 236.Ql \- 237overrides a 238.Ql \&0 239if both are used; 240.El 241.It Field Width : 242An optional digit string specifying a 243.Em field width ; 244if the output string has fewer bytes than the field width it will 245be space-padded on the left (or right, if the left-adjustment indicator 246has been given) to make up the field width (note that a leading zero 247is a flag, but an embedded zero is part of a field width); 248.It Precision : 249An optional period 250.Pq Ql \&. , 251followed by an optional digit string giving a 252.Em precision 253which specifies the number of digits to appear after the decimal point, 254for 255.Cm e 256and 257.Cm f 258formats, or the maximum number of bytes to be printed 259from a string 260.Sm off 261.Pf ( Cm b , 262.Sm on 263.Cm B , 264and 265.Cm s 266formats); if the digit string is missing, the precision is treated 267as zero; 268.It Format : 269A character which indicates the type of format to use (one of 270.Cm diouxXfFeEgGaAbBcCs ) . 271.El 272.Pp 273A field width or precision may be 274.Sq Cm \&* 275instead of a digit string. 276In this case an 277.Ar argument 278supplies the field width or precision. 279.Pp 280The format characters and their meanings are: 281.Bl -tag -width Fl 282.It Cm diouXx 283The 284.Ar argument , 285which must represent an integer constant, 286with an optional leading plus or minus sign, 287is printed as a signed decimal 288.Cm ( d 289or 290.Cm i ) , 291unsigned octal 292.Cm ( o ) , 293unsigned decimal 294.Cm ( u ) , 295or unsigned hexadecimal 296.Cm ( X 297or 298.Cm x ) . 299.It Cm fF 300The 301.Ar argument 302is printed in the style 303.Oo Li \&\- Oc Ns Ar \&\^ddd Ns Li \&\^\&. Ns Ar ddd 304where the number of 305.Ar d Ns \|'s 306after the decimal point is equal to the precision specification for 307the argument. 308If the precision is missing, 6 digits are given; if the precision 309is explicitly 0, no digits and no decimal point are printed. 310If the number is Infinity, or Not-a-Number, then 311.Ql inf 312or 313.Ql nan 314is printed for 315.Cm f 316format, and 317.Ql INF 318or 319.Ql NAN 320for 321.Cm F 322format. 323.It Cm eE 324The 325.Ar argument 326is printed in the style 327.Oo Li \&\- Oc Ns Ar \&\^d Ns Li \&. Ns Ar ddd Ns Li \&\|e\*(Pm Ns Ar \&\|dd 328where there 329is one digit before the decimal point and the number after is equal to 330the precision specification for the argument; when the precision is 331missing, 6 digits are produced. 332An upper-case 333.Ql E 334is used for an 335.Cm E 336format, and upper-case for Infinity and NaN as for 337.Cm F 338format. 339.It Cm gG 340The 341.Ar argument 342is printed in style 343.Cm f 344.Pq Cm F 345or in style 346.Cm e 347.Pq Cm E 348whichever gives full precision in minimum space. 349.It Cm aA 350The 351.Ar argument 352is treated as a floating point number, 353for which the underlying hexadecimal representation is 354printed. 355See 356.Xr printf 3 357for the details. 358.It Cm b 359Characters from the string 360.Ar argument 361are printed with backslash-escape sequences expanded. 362.Pp 363The following additional backslash-escape sequences are supported: 364.Bl -tag -width Cm 365.It Cm \ec 366Causes 367.Nm 368to ignore any remaining characters in the string operand containing it, 369any remaining string operands, and any additional characters in 370the format operand. 371.It Cm \e0 Ns Ar num 372Write an 8-bit character whose ASCII value is the 1-, 2-, or 3733-digit octal number 374.Ar num . 375.It Cm \e^ Ns Ar c 376Write the control character 377.Ar c . 378Generates characters 379.Sq \e000 380through 381.Sq \e037 , 382and 383.Sq \e177 384(from 385.Ql \e^\&? ) . 386.It Cm \eM^ Ns Ar c 387Write the control character 388.Ar c 389with the 8th bit set. 390Generates characters 391.Sq \e200 392through 393.Sq \e237 , 394and 395.Sq \e377 396(from 397.Ql \eM^\&? ) . 398.It Cm \eM\- Ns Ar c 399Write the character 400.Ar c 401with the 8th bit set. 402Generates characters 403.Sq \e241 404through 405.Sq \e376 . 406.El 407.It Cm B 408Characters from the string 409.Ar argument 410are printed with unprintable characters backslash-escaped using the 411.Sq Cm \e Ns Ar c , 412.Sq Cm \e^ Ns Ar c , 413.Sq Cm \eM^ Ns Ar c , 414or 415.Sq Cm \eM\- Ns Ar c 416formats described above. 417.It Cm c 418The first character of 419.Ar argument 420is printed. 421.It Cm C 422The 423.Ar argument , 424which must represent an integer constant, 425with an optional leading plus or minus sign, 426is treated as a wide character code point, and printed. 427.It Cm s 428Characters from the string 429.Ar argument 430are printed until the end is reached or until the number of bytes 431indicated by the precision specification is reached; if the 432precision is omitted, all characters in the string are printed. 433.El 434.Pp 435In no case does a non-existent or small field width cause truncation of 436a field; padding takes place only if the specified field width exceeds 437the actual width. 438.Sh EXIT STATUS 439.Ex -std 440.Sh SEE ALSO 441.Xr echo 1 , 442.Xr printf 3 , 443.Xr vis 3 , 444.Xr printf 9 445.Sh STANDARDS 446The 447.Nm 448utility conforms to 449.St -p1003.1-2001 . 450.Pp 451Support for the floating point formats and 452.Sq Cm \&* 453as a field width and precision 454are optional in POSIX. 455.Pp 456The behaviour of the 457.Cm \&%B 458and 459.Cm \&%C 460formats and the 461.Cm \e\(aq , 462.Cm \e\*q , 463.Cm \ee , 464.Cm \e Ns Ar num , 465and 466.Cm \e Ns Oo Cm M Oc Ns Oo Cm \- Ns Li \&\(or Ns Cm ^ Oc Ns Ar c 467escape sequences are undefined in POSIX. 468.Sh BUGS 469Since the floating point numbers are translated from ASCII to 470floating-point and then back again, floating-point precision may be lost. 471.Pp 472Hexadecimal character constants are restricted to, and should be specified 473as, two character constants. 474This is contrary to the ISO C standard but 475does guarantee detection of the end of the constant. 476.Sh NOTES 477All formats which treat the 478.Ar argument 479as a number first convert the 480.Ar argument 481from its external representation as a character string 482to an internal numeric representation, and then apply the 483format to the internal numeric representation, producing 484another external character string representation. 485One might expect the 486.Cm \&%c 487format to do likewise, but in fact it does not. 488.Pp 489To convert a string representation of a decimal, octal, or hexadecimal 490number into the corresponding character, 491using a portable invocation, 492two nested 493.Nm 494invocations may be used, in which the inner invocation 495converts the input to an octal string, and the outer 496invocation uses the octal string as part of a format. 497For example, the following command outputs the character whose code 498is 0x0a, which is a newline in ASCII: 499.Pp 500.Dl printf \*q$(printf \(aq\e\e%o\(aq 0x0a)\*q 501.Pp 502In this implementation of 503.Nm 504it is possible to achieve the same result using one invocation: 505.Pp 506.Dl printf %C 0x0a 507