linuxbashunicode

Where can I find the documents about the lone single quote before a string in bash and/or printf


Question:

$  printf '%x\n' "'🐍"
#                 ^  This single quote

Sorry if this post sounds like a newbie mistake.

But I have already tried to find documentation about this lone single quote, such as:


Context

I was trying to print Unicode characters as hexadecimal in the terminal and came across this helpful post. It shows a working example, but doesn't explain why the lone single quote is needed there.

I'm not even sure which part of the command requires the quote:

Here’s what I’ve tested:

# zsh 5.9 (x86_64-apple-darwin23.0)
$  printf '%x\n' "'🐍"                                                                                                               
1f40d

$  printf '%x\n' "🐍"
zsh: bad math expression: illegal character: ^0
0


# bash 3.2.57 @ macOS
$  printf '%x\n' "'🐍"
fffffffffffffff0

$  printf '%x\n' "🐍"
bash: printf: 🐍: invalid number
0


# bash 4.2.46 @ CentOS 7
$  printf '%x\n' "'🐍"
1f40d

$  printf '%x\n' "🐍"
-bash: printf: 🐍: invalid number
0


# bash 5.2.21 @ Ubuntu 24
$  printf '%x\n' "'🐍"
1f40d

$  printf '%x\n' "🐍"
-bash: printf: 🐍: invalid number
0

Still have no clue what's going on.

Any help or pointers to documentation would be greatly appreciated. Thank you!


Solution

  • It is documented in the printf section of the bash manual:

    Arguments to non-string format specifiers are treated as C language 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.

    and in the printf section of the zsh manual:

    With the numeric format specifiers, if the corresponding argument starts with a quote character, the numeric value of the following character is used as the number to print; otherwise the argument is evaluated as an arithmetic expression.