$ 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:
printf
, this man page only mentions single quotes in the context of number formatting with locales (if I'm understanding it correctly).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:
printf
?bash
or zsh
)?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!
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.