sprintf
Unlike printf
which sends its output to stdout
, this function takes a variable name to store its output.
If we detect that we are running under bash
version 3.1 or higher, the function is defined to use printf -v varname ...
because it is faster (bash
has not optimized for the case of varname=$( printf ... )
). On FreeBSD, /bin/sh
has been optimized for the case of varname=$( printf ... )
and it runs faster than bash's printf -v varname ...
syntax.
Usually varname=$( ... )
results in a sub-shell, impacting performance. On FreeBSD you can rely on the fact that calling printf
in a sub-shell does not impact performance as it does in bash
. Shells that neither provide printf -v varname ...
like bash
nor optimize for printf
in a sub-shell like FreeBSD may be slower.
Last updated