| 1 |
printf-about = Print output based off of the format string and proceeding arguments. |
| 2 |
printf-usage = printf FORMAT [ARGUMENT]... |
| 3 |
printf OPTION |
| 4 |
printf-after-help = basic anonymous string templating: |
| 5 |
|
| 6 |
prints format string at least once, repeating as long as there are remaining arguments |
| 7 |
output prints escaped literals in the format string as character literals |
| 8 |
output replaces anonymous fields with the next unused argument, formatted according to the field. |
| 9 |
|
| 10 |
Prints the , replacing escaped character sequences with character literals |
| 11 |
and substitution field sequences with passed arguments |
| 12 |
|
| 13 |
literally, with the exception of the below |
| 14 |
escaped character sequences, and the substitution sequences described further down. |
| 15 |
|
| 16 |
ESCAPE SEQUENCES: |
| 17 |
|
| 18 |
The following escape sequences, organized here in alphabetical order, |
| 19 |
will print the corresponding character literal: |
| 20 |
|
| 21 |
- \" double quote |
| 22 |
- \\ backslash |
| 23 |
- \\a alert (BEL) |
| 24 |
- \\b backspace |
| 25 |
- \\c End-of-Input |
| 26 |
- \\e escape |
| 27 |
- \\f form feed |
| 28 |
- \\n new line |
| 29 |
- \\r carriage return |
| 30 |
- \\t horizontal tab |
| 31 |
- \\v vertical tab |
| 32 |
- \\NNN byte with value expressed in octal value NNN (1 to 3 digits) values greater than 256 will be treated |
| 33 |
- \\xHH byte with value expressed in hexadecimal value NN (1 to 2 digits) |
| 34 |
- \\uHHHH Unicode (IEC 10646) character with value expressed in hexadecimal value HHHH (4 digits) |
| 35 |
- \\uHHHH Unicode character with value expressed in hexadecimal value HHHH (8 digits) |
| 36 |
- %% a single % |
| 37 |
|
| 38 |
SUBSTITUTIONS: |
| 39 |
|
| 40 |
SUBSTITUTION QUICK REFERENCE: |
| 41 |
|
| 42 |
Fields |
| 43 |
|
| 44 |
- %s: string |
| 45 |
- %b: string parsed for literals second parameter is max length |
| 46 |
|
| 47 |
- %c: char no second parameter |
| 48 |
|
| 49 |
- %i or %d: 64-bit integer |
| 50 |
- %u: 64 bit unsigned integer |
| 51 |
- %x or %X: 64-bit unsigned integer as hex |
| 52 |
- %o: 64-bit unsigned integer as octal |
| 53 |
|
| 54 |
second parameter is min-width, integer |
| 55 |
output below that width is padded with leading zeroes |
| 56 |
|
| 57 |
|
| 58 |
- %q: ARGUMENT is printed in a format that can be reused as shell input, escaping non-printable characters with the proposed POSIX $'' syntax. |
| 59 |
- %f or %F: decimal floating point value |
| 60 |
- %e or %E: scientific notation floating point value |
| 61 |
- %g or %G: shorter of specially interpreted decimal or SciNote floating point value. |
| 62 |
|
| 63 |
second parameter is |
| 64 |
-max places after decimal point for floating point output |
| 65 |
-max number of significant digits for scientific notation output |
| 66 |
|
| 67 |
parameterizing fields |
| 68 |
|
| 69 |
examples: |
| 70 |
|
| 71 |
printf '%4.3i' 7 |
| 72 |
|
| 73 |
It has a first parameter of 4 and a second parameter of 3 and will result in ' 007' |
| 74 |
|
| 75 |
printf '%.1s' abcde |
| 76 |
|
| 77 |
It has no first parameter and a second parameter of 1 and will result in 'a' |
| 78 |
|
| 79 |
printf '%4c' q |
| 80 |
|
| 81 |
It has a first parameter of 4 and no second parameter and will result in ' q' |
| 82 |
|
| 83 |
The first parameter of a field is the minimum width to pad the output to |
| 84 |
if the output is less than this absolute value of this width, |
| 85 |
it will be padded with leading spaces, or, if the argument is negative, |
| 86 |
with trailing spaces. the default is zero. |
| 87 |
|
| 88 |
The second parameter of a field is particular to the output field type. |
| 89 |
defaults can be found in the full substitution help below |
| 90 |
|
| 91 |
special prefixes to numeric arguments |
| 92 |
|
| 93 |
- 0: (e.g. 010) interpret argument as octal (integer output fields only) |
| 94 |
- 0x: (e.g. 0xABC) interpret argument as hex (numeric output fields only) |
| 95 |
- \': (e.g. \'a) interpret argument as a character constant |
| 96 |
|
| 97 |
HOW TO USE SUBSTITUTIONS: |
| 98 |
|
| 99 |
Substitutions are used to pass additional argument(s) into the FORMAT string, to be formatted a |
| 100 |
particular way. E.g. |
| 101 |
|
| 102 |
printf 'the letter %X comes before the letter %X' 10 11 |
| 103 |
|
| 104 |
will print |
| 105 |
|
| 106 |
the letter A comes before the letter B |
| 107 |
|
| 108 |
because the substitution field %X means |
| 109 |
'take an integer argument and write it as a hexadecimal number' |
| 110 |
|
| 111 |
Passing more arguments than are in the format string will cause the format string to be |
| 112 |
repeated for the remaining substitutions |
| 113 |
|
| 114 |
printf 'it is %i F in %s \n' 22 Portland 25 Boston 27 New York |
| 115 |
|
| 116 |
will print |
| 117 |
|
| 118 |
it is 22 F in Portland |
| 119 |
it is 25 F in Boston |
| 120 |
it is 27 F in Boston |
| 121 |
|
| 122 |
If a format string is printed but there are less arguments remaining |
| 123 |
than there are substitution fields, substitution fields without |
| 124 |
an argument will default to empty strings, or for numeric fields |
| 125 |
the value 0 |
| 126 |
|
| 127 |
AVAILABLE SUBSTITUTIONS: |
| 128 |
|
| 129 |
This program, like GNU coreutils printf, |
| 130 |
interprets a modified subset of the POSIX C printf spec, |
| 131 |
a quick reference to substitutions is below. |
| 132 |
|
| 133 |
STRING SUBSTITUTIONS: |
| 134 |
|
| 135 |
All string fields have a 'max width' parameter |
| 136 |
%.3s means 'print no more than three characters of the original input' |
| 137 |
|
| 138 |
- %s: string |
| 139 |
|
| 140 |
- %b: escaped string - the string will be checked for any escaped literals from |
| 141 |
the escaped literal list above, and translate them to literal characters. |
| 142 |
e.g. \\n will be transformed into a newline character. |
| 143 |
One special rule about %b mode is that octal literals are interpreted differently. |
| 144 |
In arguments passed by %b, pass octal-interpreted literals must be in the form of \\0NNN |
| 145 |
instead of \\NNN. (Although, for legacy reasons, octal literals in the form of \\NNN will |
| 146 |
still be interpreted and not throw a warning, you will have problems if you use this for a |
| 147 |
literal whose code begins with zero, as it will be viewed as in \\0NNN form.) |
| 148 |
|
| 149 |
- %q: escaped string - the string in a format that can be reused as input by most shells. |
| 150 |
Non-printable characters are escaped with the POSIX proposed ‘$''’ syntax, |
| 151 |
and shell meta-characters are quoted appropriately. |
| 152 |
This is an equivalent format to ls --quoting=shell-escape output. |
| 153 |
|
| 154 |
CHAR SUBSTITUTIONS: |
| 155 |
|
| 156 |
The character field does not have a secondary parameter. |
| 157 |
|
| 158 |
- %c: a single character |
| 159 |
|
| 160 |
INTEGER SUBSTITUTIONS: |
| 161 |
|
| 162 |
All integer fields have a 'pad with zero' parameter |
| 163 |
%.4i means an integer which if it is less than 4 digits in length, |
| 164 |
is padded with leading zeros until it is 4 digits in length. |
| 165 |
|
| 166 |
- %d or %i: 64-bit integer |
| 167 |
- %u: 64-bit unsigned integer |
| 168 |
- %x or %X: 64-bit unsigned integer printed in Hexadecimal (base 16). %X instead of %x means to use uppercase letters for 'a' through 'f' |
| 169 |
- %o: 64-bit unsigned integer printed in octal (base 8) |
| 170 |
|
| 171 |
FLOATING POINT SUBSTITUTIONS: |
| 172 |
|
| 173 |
All floating point fields have a 'max decimal places / max significant digits' parameter |
| 174 |
%.10f means a decimal floating point with 7 decimal places past 0 |
| 175 |
%.10e means a scientific notation number with 10 significant digits |
| 176 |
%.10g means the same behavior for decimal and Sci. Note, respectively, and provides the shortest |
| 177 |
of each's output. |
| 178 |
|
| 179 |
Like with GNU coreutils, the value after the decimal point is these outputs is parsed as a |
| 180 |
double first before being rendered to text. For both implementations do not expect meaningful |
| 181 |
precision past the 18th decimal place. When using a number of decimal places that is 18 or |
| 182 |
higher, you can expect variation in output between GNU coreutils printf and this printf at the |
| 183 |
18th decimal place of +/- 1 |
| 184 |
|
| 185 |
- %f: floating point value presented in decimal, truncated and displayed to 6 decimal places by |
| 186 |
default. There is not past-double behavior parity with Coreutils printf, values are not |
| 187 |
estimated or adjusted beyond input values. |
| 188 |
|
| 189 |
- %e or %E: floating point value presented in scientific notation |
| 190 |
7 significant digits by default. |
| 191 |
%E means use to use uppercase E for the mantissa. |
| 192 |
|
| 193 |
- %g or %G: floating point value presented in the shortest of decimal and scientific notation |
| 194 |
behaves differently from %f and %E, please see posix printf spec for full details, |
| 195 |
some examples of different behavior: |
| 196 |
|
| 197 |
- Sci Note has 6 significant digits by default |
| 198 |
- Trailing zeroes are removed |
| 199 |
- Instead of being truncated, digit after last is rounded |
| 200 |
|
| 201 |
Like other behavior in this utility, the design choices of floating point |
| 202 |
behavior in this utility is selected to reproduce in exact |
| 203 |
the behavior of GNU coreutils' printf from an inputs and outputs standpoint. |
| 204 |
|
| 205 |
USING PARAMETERS: |
| 206 |
|
| 207 |
Most substitution fields can be parameterized using up to 2 numbers that can |
| 208 |
be passed to the field, between the % sign and the field letter. |
| 209 |
|
| 210 |
The 1st parameter always indicates the minimum width of output, it is useful for creating |
| 211 |
columnar output. Any output that would be less than this minimum width is padded with |
| 212 |
leading spaces |
| 213 |
The 2nd parameter is proceeded by a dot. |
| 214 |
You do not have to use parameters |
| 215 |
|
| 216 |
SPECIAL FORMS OF INPUT: |
| 217 |
|
| 218 |
For numeric input, the following additional forms of input are accepted besides decimal: |
| 219 |
|
| 220 |
Octal (only with integer): if the argument begins with a 0 the proceeding characters |
| 221 |
will be interpreted as octal (base 8) for integer fields |
| 222 |
|
| 223 |
Hexadecimal: if the argument begins with 0x the proceeding characters will be interpreted |
| 224 |
will be interpreted as hex (base 16) for any numeric fields |
| 225 |
for float fields, hexadecimal input results in a precision |
| 226 |
limit (in converting input past the decimal point) of 10^-15 |
| 227 |
|
| 228 |
Character Constant: if the argument begins with a single quote character, the first byte |
| 229 |
of the next character will be interpreted as an 8-bit unsigned integer. If there are |
| 230 |
additional bytes, they will throw an error (unless the environment variable POSIXLY_CORRECT |
| 231 |
is set) |
| 232 |
|
| 233 |
printf-error-missing-operand = missing operand |
| 234 |
printf-warning-ignoring-excess-arguments = ignoring excess arguments, starting with { $arg } |
| 235 |
printf-help-version = Print version information |
| 236 |
printf-help-help = Print help information |
| 237 |
|
| 238 |
# Diagnostic advice: shown under the caret when a format fails to parse |
| 239 |
printf-diag-help-spec = %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %% |
| 240 |
printf-diag-help-escape-hex = \x takes one or two hexadecimal digits, \u takes four and \U takes eight |
| 241 |
printf-diag-help-unicode = code points between D800 and DFFF or above 10FFFF are not Unicode characters |