| `%b` | Print the associated argument while interpreting backslash escapes in there |
| `%q` | Print the associated argument **shell-quoted**, reusable as input |
| `%d` | Print the associated argument as **signed decimal** number |
| `%i` | Same as `%d` |
| `%o` | Print the associated argument as **unsigned octal** number |
| `%u` | Print the associated argument as **unsigned decimal** number |
| `%x` | Print the associated argument as **unsigned hexadecimal** number with lower-case hex-digits (a-f) |
| `%X` | Same as `%x`, but with upper-case hex-digits (A-F) |
| `%f` | Interpret and print the associated argument as **floating point** number |
| `%e` | Interpret the associated argument as **double**, and print it in `<N>±e<N>` format |
| `%E` | Same as `%e`, but with an upper-case `E` in the printed format |
| `%g` | Interprets the associated argument as **double**, but prints it like `%f` or `%e` |
| `%G` | Same as `%g`, but print it like `%E` |
| `%c` | Interprets the associated argument as **char**: only the first character of a given argument is printed |
| `%s` | Interprets the associated argument literally as string |
| `%n` | Assigns the number of characters printed so far to the variable named in the corresponding argument. Can't specify an array index. If the given name is already an array, the value is assigned to the zeroth element. |
| `%a` | Interprets the associated argument as **double**, and prints it in the form of a C99 [hexadecimal floating-point literal](http://www.exploringbinary.com/hexadecimal-floating-point-constants/). |
| `%A` | Same as `%a`, but print it like `%E` |
| `%(FORMAT)T` | output the date-time string resulting from using `FORMAT` as a format string for `strftime(3)`. The associated argument is the number of seconds since Epoch, or `-1` (current time) or `-2` (shell startup time). If no corresponding argument is supplies, the current time is used as default |
| `%%` | No conversion is done. Produces a `%` (percent sign) |
Some of the mentioned format specifiers can modify their behaviour by
getting a format modifier:
### Modifiers
To be more flexible in the output of numbers and strings, the `printf`
command allows format modifiers. These are specified **between** the
introductory `%` and the character that specifies the format:
printf "%50s\n""This field is 50 characters wide..."
| `<N>` | **Any number**: Specifies a **minimum field width**, if the text to print is shorter, it's padded with spaces, if the text is longer, the field is expanded |
| `.` | **The dot**: Together with a field width, the field is **not** expanded when the text is longer, the text is truncated instead. "`%.s`" is an undocumented equivalent for "`%.0s`", which will force a field width of zero, effectively hiding the field from output |
| `*` | **The asterisk**: the width is given as argument before the string or number. Usage (the "`*`" corresponds to the "`20`"): `printf "%*s\n" 20 "test string"` |
| `#` | "Alternative format" for numbers: see table below |
| `-` | **Left-bound** text printing in the field (standard is **right-bound**) |
| `0` | Pads numbers with zeros, not spaces |
| `<space>` | Pad a positive number with a space, where a minus (`-`) is for negative numbers |
| `+` | Prints all numbers **signed** (`+` for positive, `-` for negative) |
| `'` | For decimal conversions, the thousands grouping separator is applied to the integer portion of the output according to the current LC_NUMERIC |
| `%#o` | The octal number is printed with a leading zero, unless it's zero itself |
| `%#x`, `%#X` | The hex number is printed with a leading "`0x`"/"`0X`", unless it's zero |
| `%#g`, `%#G` | The float number is printed with **trailing zeros** until the number of digits for the current precision is reached (usually trailing zeros are not printed) |
| all number formats except `%d`, `%o`, `%x`, `%X` | Always print a decimal point in the output, even if no digits follow it |
#### Precision
The precision for a floating- or double-number can be specified by using
`.<DIGITS>`, where `<DIGITS>` is the number of digits for precision. If
`<DIGITS>` is an asterisk (`*`), the precision is read from the argument
that precedes the number to print, like (prints 4,3000000000):
printf "%.*f\n" 10 4,3
The format `.*N` to specify the N'th argument for precision does not
work in Bash.
For strings, the precision specifies the maximum number of characters to
print (i.e., the maximum field width). For integers, it specifies the
number of digits to print (zero-padding!).
### Escape codes
These are interpreted if used anywhere in the format string, or in an
| `\c` | Terminate output similarly to the `\c` escape used by `echo -e`. printf produces no additional output after coming across a `\c` escape in a `%b` argument. |
- Backslashes in the escapes: `\'`, `\"`, and `\?` are not removed.
- Octal escapes beginning with `\0` may contain up to four digits.
(POSIX specifies up to three).
These are also respects in which `%b` differs from the escapes used by