Tuesday, June 5, 2012

Pipe to/from Clipboard

Is it possible to pipe to/from the clipboard in bash? Whether it's 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

Source: Tips4all


  1. You're a little ambiguous. I expect you're probably a Linux user inside X who wants to put stuff in the X PRIMARY clipboard.

    It's important to understand that bash doesn't have a clipboard. There is no such thing as "the" clipboard, because bash can run on Windows, Mac OS X, lots of other OSes, inside X, outside X, ... Not to mention that X itself has three different clipboards itself. There's a wealth of clipboards you could be dealing with. Usually the clipboard you want to talk to has a utility that lets you talk to it.

    In case of X, yes, there's xclip (and others).

    If you're trying to talk to the Mac OS X clipboard, there's pbcopy.

    If you're in Linux terminal mode (no X) then maybe you need to look into gpm.

    There's also GNU screen which has a clipboard. To put stuff in there, look at the screen command "readreg".

    For Windows, read /dev/clipboard (thanks glenn).

  2. 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

  3. There are different clipboards in linux, the X server has one, the window manager might have another one, etc. There is no standard device.

    Oh, yes, on CLI, the screen program has its own clipboard as well, as do some other applications like emacs and vi.

    In X, you can use xclip.

    You can check this thread for other possible answers:

  4. On Mac OS X you might find these command line tools handy


  5. On Windows (with Cygwin) try
    cat /dev/clipboard or echo "foo" > /dev/clipboard as mentioned in this article.