16

Is there a way to type the null character in the terminal?

I would like to do something like:

this is a sentence (null) test123
Potherca
  • 13,207
  • 5
  • 76
  • 94
functionalCode
  • 503
  • 1
  • 8
  • 25

4 Answers4

15

In Linux, any special character can be literally inserted on the terminal by pressing Ctrl+v followed by the actual symbol. null is usually ^@ where ^ stands for Ctrl and @ for whatever combination on your keyboard layout that produces @.

So on my keyboard I do: Ctrl+v followed by Ctrl+Shift+@ and I get a ^@ symbol with a distinguished background color. This means it's a special character and not just ^ and @ typed in.

Edit: Several years later and a few input variations implemented by different terminals using keyboard layouts that require pressing Shift to access @.

  • Ctrl+v followed by Ctrl+Shift+@
  • Ctrl+v followed by Shift+@ without releasing Ctrl.
  • Ctrl+Shift+v followed by @ without releasing Ctrl+Shift.
  • Ctrl+Shift release Shift and re-press Shift keeping both Ctrl+Shift pressed followed by v and finally @. Seen in some terminals that implement a special input on Ctrl+Shift.
unode
  • 9,321
  • 4
  • 33
  • 44
  • 3
    You're talking about `Vim`. Asker's talking about the `Shell`. Vim is the only one that accepts the NUL combo, and is also the only one that colors its control chars. – Braden Best Nov 17 '15 at 19:43
  • 1
    @B1KMusic Sorry but no, I'm not talking about `Vim`. I'm talking about plain `xterm`, both `bash` and `zsh`. – unode Nov 18 '15 at 23:06
  • Nope, doesn't work in bash. Only zsh and vim. I tested it in tty, xterm, st, rxvt and gnome terminal. – Braden Best Nov 18 '15 at 23:10
  • Bash version 4.3.11, stock from Ubuntu repo – Braden Best Nov 18 '15 at 23:12
  • 1
    I did just fine a weird quirk, though. If you run, e.g. `while true; do true; done` and then hit `Ctrl + Space` while you don't have control, it will print null characters. Not really useful, but interesting behavior nonetheless – Braden Best Nov 19 '15 at 04:54
  • 3
    Typically Ctrl-Space enters a null character -- depending on the terminal emulator. But I've just run into a problem where xterm under Ubuntu 14.04.03 LTS doesn't recognize either Ctrl-Space or Ctrl-@ (no input is generated for either). But it works under Linux Mint 17.1 and 17.2. Still trying to track down the cause. (I use ^@ as the command character for `screen`.) – Keith Thompson Dec 16 '15 at 19:42
  • @BradenBest `bash` has nothing to do with it -- it's the terminal that reads input and sends the key codes. `bash` may be ignoring it, but pretty much every terminal in existence sends `NUL` for Ctrl+@. Run `showkey -a` and try it. – Craig Barnes May 30 '18 at 13:37
  • @CraigBarnes appreciated, but... three years. Just saying. I've already known about that for a while, and frankly don't even remember writing the above comments. – Braden Best Jun 01 '18 at 08:27
  • No worries. Just thought I'd clarify for anyone else who finds this comment and gets the impression that using Ctrl+@ to send `NUL` isn't portable. – Craig Barnes Jun 01 '18 at 08:47
  • So there's no way to get this working on gnome-terminal? @BradenBest I'm on Ubuntu 18.04.3 and I tried all the methods mentioned in the answer, every key combo you can think of, but it didn't work. – Shayan Oct 15 '19 at 15:26
  • I can't even get a gnome-terminal running at the moment (which is fine because I prefer [st](https://st.suckless.org/) anyway), so I dunno what to tell you. – Braden Best Oct 16 '19 at 15:57
10
$ echo -e "this is a sentence \0 test123"
this is a sentence  test123

The null here ^^ IS NOT visible

$ echo -e "this is a sentence \0 test123" | cat --show-nonprinting
this is a sentence ^@ test123

But it IS here ^^

But maybe you did not want this for a script?

Potherca
  • 13,207
  • 5
  • 76
  • 94
Oskar N.
  • 8,427
  • 2
  • 23
  • 21
7

Apparently you can type this character with ^@ on some character sets. This wikipedia article on the null character may be helpful.

tjarratt
  • 1,692
  • 9
  • 17
3

As with the abort command (Ctrl-C), in most terminals just hit Ctrl-@ (with the use of Shift on my keyboard).

phuclv
  • 37,963
  • 15
  • 156
  • 475
AXE Labs
  • 4,051
  • 4
  • 29
  • 29