Pipe to/from the clipboard in Bash script

2019-01-03 19:08发布

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

23条回答
一夜七次
2楼-- · 2019-01-03 19:37
  xsel -b

Does the job for x11, it is mostly already installed. A look in the man page of xsel is worth the effort.

查看更多
干净又极端
3楼-- · 2019-01-03 19:38

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

查看更多
再贱就再见
4楼-- · 2019-01-03 19:38

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)

查看更多
做自己的国王
5楼-- · 2019-01-03 19:40

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

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-03 19:41

This is a simple Python script that does just what you need:

#!/usr/bin/python

import sys

# Clipboard storage
clipboard_file = '/tmp/clipboard.tmp'

if(sys.stdin.isatty()): # Should write clipboard contents out to stdout
    with open(clipboard_file, 'r') as c:
        sys.stdout.write(c.read())
elif(sys.stdout.isatty()): # Should save stdin to clipboard
    with open(clipboard_file, 'w') as c:
        c.write(sys.stdin.read())

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

echo "Hello World" | clip

And you can pipe what's in your clipboard to some other program...

clip | cowsay
 _____________
< Hello World >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Running it by itself will simply output what's in the clipboard.

查看更多
趁早两清
7楼-- · 2019-01-03 19:41

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:

$ echo "hello world" > ~/clip
$ echo `cat ~/clip`
hello world
查看更多
登录 后发表回答