Commands
printf 'ABC$def%hx\n' 10 # output: ABC$defa
printf 'ABC$def%s\n' 10 # output: ABC$def10
work as expected.... but I cannot find any info about %h.
Is it available on all systems?
Is it available on all systems?
You are using printf
command in shell. This command might have the h
format length modifier , when:
you are using Bash, it will be builtin printf
command built in in Bash inside it.
"h" is there at least 16 years ago, Bash 2.02
https://github.com/bminor/bash/blame/master/builtins/printf.def#L187
you are using busybox
ash
, then:
'h' was added 17 years ago from the project beginning
https://github.com/mirror/busybox/blame/master/coreutils/printf.c#L351
you are using GNU coreutils
, then:
'h' was added 33 years ago
The info about h
comes from C standard:
h
was there in printf
C in 1989 https://port70.net/~nsz/c/c89/c89-draft.html#4.9.6.1h
does in C programming language, check https://en.cppreference.com/w/c/io/fprintfHowever, h
might not be supported, when:
using Kernel dash shell printf
builtin
Ubuntu uses dash by default
https://web.git.kernel.org/pub/scm/utils/dash/dash.git/tree/src/bltin/printf.c#n263
# printf "%hhx" 10
dash: 2: printf: %h: invalid directive
using FreeBSD
I do not see length modifier mentioned, except for L for floats
and I do not see it in source code https://github.com/lattera/freebsd/blob/master/usr.bin/printf/printf.c#L328
and I tested (side note: vagrant booted with Bash on freebsd anyway)
vagrant@freebsd13:~ $ printf 'ABC$def%hx\n' 10
printf: illegal format character h
on macOS /usr/bin/printf
does not support it, which most probably is the same version from BSD
and reading POSIX I do not see length modifier mentioned:
I say, if working with Bash, it will be available.
But it might be not available on Ubuntu and BSD. I think MacOS has Bash3 anyway.
Fun fact: what is the output of the following code printf "%hjLltzhhhjjjLLLLtttzzzx\n" 10
in Bash?
printf format specifier %h; is it standard?
There is no %h
sole format specifier. The letter h
is used as a modifier of the format specifier following it. In C programming language, %hhx
%hx
%x
%llx
%lx
%zx
%jx
all take different arguments type. See table in https://en.cppreference.com/w/c/io/fprintf for explanation. In shell however it does not matter at all, and all implementation just ignore such modifiers.