This are some methods that have not been mentioned before:
printf '\x40'
printf '\u40'
echo -e '\u40'
echo $'\u40'
echo 40 | xxd -p -r
perl -le 'print chr 64'
python <<<'print chr(64)'
python3 -c 'print(chr(64))'
php -r 'echo chr(64);'
[1]
The 40
value is the hexadecimal value of an ASCII character @
. Or 64
in decimal (used later). That is expressed in \x40
, or read as hexadecimal (\x
) value 40
. The command printf converts it to a byte of such value and the console prints a @
.
And it is also the Unicode code point value of the same character @
. It so happens that Unicode code points and ASCII character values are exactly the same (by design) up to 127 (hex 7F
or octal 177
).
There is, however, a difference between \x40
(or \100
) and \u40
. The latter will always produce an UNICODE code point, no matter what locale, language or condition is set for the OS or computer that runs the code. While printf '\100'
would output a space. And, also by design, code points in UNICODE have been assigned as static values. It is expected that they won't change in the future. In short, a \u40
should produce a @
even on an EBCDIC-based system (for example). For ASCII values that might seem as an small gain, but for higher code point characters, it is a real advantage as \u2225
should always produce parallel
(∥
) no matter the locale or language being used.
Support for \uxxxx
was first added in GNU printf (the standalone utility) in 2000. Then to zsh's print/printf/echo/$'...' in 2003 and a few other shells later. The $'\uxxxx' syntax is even planned for inclusion in POSIX. You need the 4 digits in the standalone printf: printf '\u0040'
. In bash, ksh93 and zsh from 1 to 4 digits are allowed. A \Uxxxxxxxx
is required to use up to 8 hex digits.
[1] Initial Idea from a (now erased) comment of Stéphane Chazelas
localectl set-keymap us
in many modern distros).@
part of your prompt would make it always accessible.@
symbol is probably where"
is. Or useloadkeys
or something to set your actual keyboard layout.