linuxbashprintfformat-specifiers

printf format specifier %h; is it standard?


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?


Solution

  • Is it available on all systems?

    You are using printf command in shell. This command might have the h format length modifier , when:

    The info about h comes from C standard:

    However, h might not be supported, when:

    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.