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