Entering a Null Byte Interactively
A nice little fact you learn along the way when you’re always in the terminal is
that the characters that appear when you press control-C, namely ^C, carry more
meaning than one might think initially. The caret (^) can be thought of the value
0b100000 (that’s 127 in decimal) XOR’ed with the ASCII value of C. In this case, it
would yield ASCII 3, which is exactly what programs that run in raw mode (like vim) would receive.
If you take a look at the ASCII man page (man 7 ascii), you’ll see
printable characters on the right column. You can apply the same logic to many of them, i.e.
^@, ^A, …, ^Z. There’s an exception, ^?, which stands for backspace. That’s right, you can
press control-? (control-shift-/ on a qwerty keyboard) in a “normal” terminal and it should do exactly
the same as pressing backspace. On my system, even control-8 works, but I digress.
With that nugget of information in mind, we can answer the question of how to enter a null byte
in the terminal: control-@ (control-shift-2 on qwerty)
But hold on, there are more ways:
control-spacecontrol-backtickcontrol-2
That’s 4 ways to insert a null byte! Note that these methods don’t always work, because the terminal emulator (Alacritty, ghostty, etc.) and/or terminal multiplexer might not support certain inputs or might do some other processing. One case where it never works for me is nested tmux sessions.
Testing that it works#
stty -icanon;head -c1|xxd
stty -icanon;printf "\nASCII value in hex: %02X\n" "'$(head -c1)"