Linux – Is is possible to copy binary data containing a NUL character to the X clipboard

clipboardlinuxxorg

If I do either of:

printf 'a\0b' | xsel
printf 'a\0b' | xsel -b
printf 'a\0b' | xclip
printf 'a\0b' | xclip -selection c

and then paste from the corresponding selection (PRIMARY or CLIPBOARD) on either of:

  • browser textareas
  • text editors
  • pipe like in xsel | hexdump

only a gets output.

So, is it possible to store binary data on the clipboard and then paste it later? How?

I originally wanted to do this to save images to the clipboard, which would also solve:

Best Answer

The clipboard does contain the full string:

$ printf 'a\0b' | hexdump -c
0000000   a  \0   b                                                    
0000003
$ printf 'a\0b' | xclip && xclip -out | hexdump -c
0000000   a  \0   b                                                    
0000003

So xclip -out will print the input verbatim, NUL characters and all. Most likely the desktop clipboard integration stops output at NUL characters. One obvious problem with pasting NUL characters is that it wouldn't be visible to the user, and as such would likely cause unexpected behaviour. Not to mention that even popular software can fail to handle NUL characters properly.

Related Question