Search code examples
linuxbashshellascii

That is so strange ,WHY


It is so strange!!!The first command work well ,but the second output error.forgive my poor english

 1.printf '%d\n' "'a"
 2.printf '%d\n' "a"

enter image description here


Solution

  • From the bash man page, (in the pretty lengthy section titled SHELL BUILTIN COMMANDS) about command printf:

    Arguments to non-string format specifiers are treated as C constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character.

    So you would also have succeeded with

    printf '%d\n' \"a