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.
Drawbacks:
printf
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.
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.
printf approach.
It is easy to make editing mistakes during this process.
env printf uses a subshell.
This is slower than the printf_gettext approach.