manpagez: man pages & more
info gettext
Home | html | info | man

File: gettext.info,  Node: The printf approach,  Next: The printf_gettext approach,  Prev: sh_approach.php">The gettext.sh approach,  Up: sh

16.5.15.3 The ‘printf’ approach
...............................

   This approach uses a POSIX:2024 compliant ‘printf’ command.

   Here's an example that references a shell variable ‘pid’:
     env printf "`gettext \"Running as process number %u.\"`"'\n' $pid

   An example is available in the ‘hello-2.sh’ file in the
‘examples/hello-sh’ directory.

Advantages and Drawbacks
------------------------

   Drawbacks:
   • Portability: A POSIX compliant ‘printf’
     (https://pubs.opengroup.org/onlinepubs/9799919799/utilities/printf.html)
     command is required, such as the one from GNU coreutils 9.6 or
     newer, from FreeBSD 11 or newer, or from Solaris 11 OpenIndiana or
     Solaris 11 OmniOS. At the time of this writing (2025), the only
     known shells whose ‘printf’ built-in is POSIX compliant are ‘ksh93’
     and ‘zsh’; ‘bash’ and ‘dash’ are not.  For this reason, the code
     needs to use ‘env printf’, not ‘printf’, so as to avoid invoking
     the shell's ‘printf’ built-in.
   • The strings passed to the ‘gettext’ et al. program inside a
     subshell must not start nor end with a newline.  This means, in
     order to add a newline after a message, you either need to append a
     newline to the format string argument of ‘printf’, or to add an
     explicit invocation of ‘echo’.
   • Some amount of backslashing is involved, see *note Preparing for
     printf::.  It is easy to make editing mistakes during this process.
   • Speed: Each invocation of ‘env printf’ uses a subshell.  This is
     slower than the ‘printf_gettext’ approach.

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.