2019-03-31 09:05:09 +00:00
.. _cmd-printf:
2018-12-17 01:39:33 +00:00
printf - display text according to a format string
2019-01-03 04:10:47 +00:00
==================================================
2018-12-17 01:39:33 +00:00
2018-12-18 01:58:24 +00:00
Synopsis
--------
2018-12-16 21:08:41 +00:00
docs synopsis: add HTML highlighing and automate manpage markup
Recent synopsis changes move from literal code blocks to
[RST line blocks]. This does not translate well to HTML: it's not
rendered in monospace, so aligment is lost. Additionally, we don't
get syntax highlighting in HTML, which adds differences to our code
samples which are highlighted.
We hard-wrap synopsis lines (like code blocks). To align continuation
lines in manpages we need [backslashes in weird places]. Combined with
the **, *, and `` markup, it's a bit hard to get the alignment right.
Fix these by moving synopsis sources back to code blocks and compute
HTML syntax highlighting and manpage markup with a custom Sphinx
extension.
The new Pygments lexer can tokenize a synopsis and assign the various
highlighting roles, which closely matches fish's syntax highlighing:
- command/keyword (dark blue)
- parameter (light blue)
- operator like and/or/not/&&/|| (cyan)
- grammar metacharacter (black)
For manpage output, we don't project the fish syntax highlighting
but follow the markup convention in GNU's man(1):
bold text type exactly as shown.
italic text replace with appropriate argument.
To make it easy to separate these two automatically, formalize that
(italic) placeholders must be uppercase; while all lowercase text is
interpreted literally (so rendered bold).
This makes manpages more consistent, see string-join(1) and and(1).
Implementation notes:
Since we want manpage formatting but Sphinx's Pygments highlighing
plugin does not support manpage output, add our custom "synopsis"
directive. This directive parses differently when manpage output is
specified. This means that the HTML and manpage build processes must
not share a cache, because the parsed doctrees are cached. Work around
this by using separate cache locations for build targets "sphinx-docs"
(which creates HTML) and "sphinx-manpages". A better solution would
be to only override Sphinx's ManualPageBuilder but that would take a
bit more code (ideally we could override ManualPageWriter but Sphinx
4.3.2 doesn't really support that).
---
Alternative solution: stick with line blocks but use roles like
:command: or :option: (or custom ones). While this would make it
possible to produce HTML that is consistent with code blocks (by adding
a bit of CSS), the source would look uglier and is harder to maintain.
(Let's say we want to add custom formatting to the [|] metacharacters
in HTML. This is much easier with the proposed patch.)
---
[RST line blocks]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#line-blocks
[backslashes in weird places]: https://github.com/fish-shell/fish-shell/pull/8626#discussion_r782837750
2022-01-09 14:09:46 +00:00
.. synopsis ::
printf FORMAT [ARGUMENT ...]
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Description
2019-01-03 04:10:47 +00:00
-----------
2021-07-16 16:21:41 +00:00
.. only :: builder_man
NOTE: This page documents the fish builtin `` printf `` .
2024-03-16 15:45:00 +00:00
To see the documentation on any non-fish versions, use `` command man printf `` .
2021-07-16 16:21:41 +00:00
2022-03-11 16:18:41 +00:00
`` 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.
2018-12-16 21:08:41 +00:00
2022-03-11 16:18:41 +00:00
The *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.
2018-12-16 21:08:41 +00:00
2022-09-23 17:57:49 +00:00
Unlike :doc: `echo <echo>` , `` printf `` does not append a new line unless it is specified as part of the string.
2018-12-16 21:08:41 +00:00
2020-05-18 17:59:45 +00:00
It doesn't support any options, so there is no need for a `` -- `` separator, which makes it easier to use for arbitrary input than `` echo `` . [#]_
2018-12-16 21:08:41 +00:00
2020-05-18 17:59:45 +00:00
Format Specifiers
-----------------
Valid format specifiers are taken from the C library function `` printf(3) `` :
2018-12-16 21:08:41 +00:00
2020-05-18 17:59:45 +00:00
- `` %d `` or `` %i `` : Argument will be used as decimal integer (signed or unsigned)
2018-12-16 21:08:41 +00:00
2019-12-20 15:58:58 +00:00
- `` %o `` : An octal unsigned integer
2018-12-16 21:08:41 +00:00
2020-05-18 17:59:45 +00:00
- `` %u `` : An unsigned decimal integer - this means negative numbers will wrap around
2018-12-16 21:08:41 +00:00
2019-12-20 15:58:58 +00:00
- `` %x `` or `` %X `` : An unsigned hexadecimal integer
2018-12-16 21:08:41 +00:00
2020-05-18 17:59:45 +00:00
- `` %f `` , `` %g `` or `` %G `` : A floating-point number. `` %f `` defaults to 6 places after the decimal point (which is locale-dependent - e.g. in de_DE it will be a `` , `` ). `` %g `` and `` %G `` will trim trailing zeroes and switch to scientific notation (like `` %e `` ) if the numbers get small or large enough.
2018-12-16 21:08:41 +00:00
2019-12-20 15:58:58 +00:00
- `` %e `` or `` %E `` : A floating-point number in scientific (XXXeYY) notation
2018-12-16 21:08:41 +00:00
2019-12-20 15:58:58 +00:00
- `` %s `` : A string
2018-12-16 21:08:41 +00:00
2019-12-20 15:58:58 +00:00
- `` %b `` : As a string, interpreting backslash escapes, except that octal escapes are of the form \0 or \0ooo.
2018-12-16 21:08:41 +00:00
2018-12-19 20:02:45 +00:00
`` %% `` signifies a literal "%".
2018-12-16 21:08:41 +00:00
2020-10-26 18:19:05 +00:00
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 `` 2>/dev/null `` .
2018-12-16 21:08:41 +00:00
2020-09-28 16:42:02 +00:00
A number between the `` % `` and the format letter specifies the width. The result will be left-padded with spaces.
2020-05-18 17:59:45 +00:00
Backslash Escapes
-----------------
2018-12-16 21:08:41 +00:00
printf also knows a number of backslash escapes:
2020-05-18 17:59:45 +00:00
2018-12-19 20:02:45 +00:00
- `` \" `` double quote
- `` \\ `` backslash
- `` \a `` alert (bell)
- `` \b `` backspace
- `` \c `` produce no further output
- `` \e `` escape
- `` \f `` form feed
- `` \n `` new line
- `` \r `` carriage return
- `` \t `` horizontal tab
- `` \v `` vertical tab
- `` \ooo `` octal number (ooo is 1 to 3 digits)
- `` \xhh `` hexadecimal number (hhh is 1 to 2 digits)
- `` \uhhhh `` 16-bit Unicode character (hhhh is 4 digits)
- `` \Uhhhhhhhh `` 32-bit Unicode character (hhhhhhhh is 8 digits)
2020-05-18 17:59:45 +00:00
Errors and Return Status
------------------------
2020-10-26 18:19:05 +00:00
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.
2020-05-18 17:34:53 +00:00
It will also return non-zero if no argument at all was given, in which case it will print nothing.
2018-12-19 20:02:45 +00:00
2020-05-18 17:59:45 +00:00
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 `` command printf `` .
2018-12-16 21:08:41 +00:00
2018-12-19 02:44:30 +00:00
Example
2019-01-03 04:10:47 +00:00
-------
2018-12-16 21:08:41 +00:00
2018-12-19 03:14:04 +00:00
::
2020-08-26 16:29:03 +00:00
printf '%s\t%s\n' flounder fish
2018-12-19 03:14:04 +00:00
2018-12-16 21:08:41 +00:00
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.
2018-12-19 03:14:04 +00:00
::
2019-02-24 23:01:16 +00:00
printf '%s: %d' "Number of bananas in my pocket" 42
2018-12-19 03:14:04 +00:00
2020-10-26 18:19:05 +00:00
Will print "Number of bananas in my pocket: 42", `without` a newline.
2020-03-01 12:43:14 +00:00
See Also
--------
2022-09-23 17:57:49 +00:00
- the :doc: `echo <echo>` command, for simpler output
2020-05-18 17:59:45 +00:00
Footnotes
---------
2021-08-10 17:46:39 +00:00
.. [#] In fact, while fish's `` echo `` supports `` -- `` , POSIX forbids it, so other implementations can't be used if the input contains anything starting with `` - `` .