Pipe to/from the clipboard in Bash script


Question

Is it possible to pipe to/from the clipboard in Bash?

Whether it is piping to/from a device handle or using an auxiliary application, I can't find anything.

For example, if /dev/clip was a device linking to the clipboard we could do:

cat /dev/clip        # Dump the contents of the clipboard
cat foo > /dev/clip  # Dump the contents of "foo" into the clipboard
1
838
4/14/2018 3:51:59 AM

Accepted Answer

There's a wealth of clipboards you could be dealing with. I expect you're probably a Linux user who wants to put stuff in the X Windows primary clipboard. Usually, the clipboard you want to talk to has a utility that lets you talk to it.

In the case of X, there's xclip (and others). xclip -selection c will send data to the clipboard that works with Ctrl + C, Ctrl + V in most applications.

If you're on Mac OS X, there's pbcopy.

If you're in Linux terminal mode (no X) then look into gpm or screen which has a clipboard. Try the screen command readreg.

Under Windows 10+ or cygwin, use /dev/clipboard or clip.

770
7/31/2019 11:50:52 PM

Make sure you are using alias xclip="xclip -selection c" otherwise you can't just use to Ctrl+v to paste it back in a different place.

echo test | xclip    

Ctrl+v === test


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon