• 1
name Punditsdkoslkdosdkoskdo

Pipe to/from the clipboard in Bash script

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

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. 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). xclip -selection c will send data to the clipboard that works with Ctrl-C, Ctrl-V in most applications.

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

Under Windows/cygwin, use /dev/clipboard or clip for newer versions of Windows (at least Windows 10).

  • 1
Reply Report