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
Does the job for x11, it is mostly already installed. A look in the man page of xsel is worth the effort.
On Windows (with Cygwin) try
cat /dev/clipboard
orecho "foo" > /dev/clipboard
as mentioned in this article.Copy and paste to clipboard in Windows (Cygwin):
see:
$ clip.exe -?
CLIP Description: Redirects output of command line tools to the Windows clipboard. This text output can then be pasted into other programs. Parameter List: /? Displays this help message. Examples: DIR | CLIP Places a copy of the current directory listing into the Windows clipboard. CLIP < README.TXT Places a copy of the text from readme.txt on to the Windows clipboard.
Also exists getclip (can be used instead of shift+ins!), putclip (echo oaeuoa | putclip.exe to put it into clip)
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, becausebash
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 thescreen
command "readreg
".Under Windows/cygwin, use
/dev/clipboard
orclip
for newer versions of Windows (at least Windows 10).This is a simple Python script that does just what you need:
Save this as an executable somewhere in your path (I saved it to
/usr/local/bin/clip
. You can pipe in stuff to be saved to your clipboard...And you can pipe what's in your clipboard to some other program...
Running it by itself will simply output what's in the clipboard.
If you're like me and run on a linux server without root privileges and there's no xclip or gpm you could workaround this issue by just using a temporary file. For example: