printf - display text according to a format string¶
printf FORMAT [ARGUMENT ...]
printf uses the format string FORMAT to print the ARGUMENT arguments. This means that it takes format specifiers in the format string and replaces each with an argument.
format argument is re-used as many times as necessary to convert all of the given arguments. So
printf %s\n flounder catfish clownfish shark will print four lines.
printf does not append a new line unless it is specified as part of the string.
It doesn't support any options, so there is no need for a
-- separator, which makes it easier to use for arbitrary input than
Valid format specifiers are taken from the C library function
%i: Argument will be used as decimal integer (signed or unsigned)
%o: An octal unsigned integer
%u: An unsigned decimal integer - this means negative numbers will wrap around
%X: An unsigned hexadecimal integer
%G: A floating-point number.
%fdefaults to 6 places after the decimal point (which is locale-dependent - e.g. in de_DE it will be a
%Gwill trim trailing zeroes and switch to scientific notation (like
%e) if the numbers get small or large enough.
%E: A floating-point number in scientific (XXXeYY) notation
%s: A string
%b: As a string, interpreting backslash escapes, except that octal escapes are of the form 0 or 0ooo.
%% signifies a literal "%".
Conversion can fail, e.g. "102.234" can't losslessly convert to an integer, causing printf to print an error. If you are okay with losing information, silence errors with
A number between the
% and the format letter specifies the width. The result will be left-padded with spaces.
printf also knows a number of backslash escapes:
\cproduce no further output
\ooooctal number (ooo is 1 to 3 digits)
\xhhhexadecimal number (hhh is 1 to 2 digits)
\uhhhh16-bit Unicode character (hhhh is 4 digits)
\Uhhhhhhhh32-bit Unicode character (hhhhhhhh is 8 digits)
Errors and Return Status¶
If the given argument doesn't work for the given format (like when you try to convert a number like 3.141592 to an integer), printf prints an error, to stderr. printf will then also return non-zero, but will still try to print as much as it can.
It will also return non-zero if no argument at all was given, in which case it will print nothing.
This printf has been imported from the printf in GNU Coreutils version 6.9. If you would like to use a newer version of printf, for example the one shipped with your OS, try
printf '%s\t%s\n' flounder fish
Will print "flounder fish" (separated with a tab character), followed by a newline character. This is useful for writing completions, as fish expects completion scripts to output the option followed by the description, separated with a tab character.
printf '%s: %d' "Number of bananas in my pocket" 42
Will print "Number of bananas in my pocket: 42", without a newline.