Linux 管道到/从 Bash 脚本中的剪贴板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/749544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pipe to/from the clipboard in Bash script
提问by moinudin
Is it possible to pipe to/from the clipboard in Bash?
是否可以通过管道传入/传出 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:
例如,如果/dev/clip
有一个设备链接到剪贴板,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
采纳答案by lhunath
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.
您可以处理大量剪贴板。我希望您可能是一个 Linux 用户,想要将内容放入 X Windows 主剪贴板。通常,您要与之交谈的剪贴板有一个实用程序可以让您与之交谈。
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 + Vin most applications.
在 X 的情况下,有xclip
(和其他)。在大多数应用程序中,xclip -selection c
将数据发送到与 一起使用的剪贴板。Ctrl + CCtrl + V
If you're on Mac OS X, there's pbcopy
.
如果您使用的是 Mac OS X,则有pbcopy
.
If you're in Linux terminal mode (no X) then look into gpm
or screenwhich has a clipboard. Try the screen
command readreg
.
如果您处于 Linux 终端模式(无 X),请查看gpm
或筛选具有剪贴板的屏幕。试试这个screen
命令readreg
。
Under Windows 10+ or cygwin, use /dev/clipboard
or clip
.
在 Windows 10+ 或 cygwin 下,使用/dev/clipboard
或clip
。
回答by Sunny Milenov
There are different clipboards in Linux; the X server has one, the window manager might have another one, etc. There is no standard device.
Linux 中有不同的剪贴板;X 服务器有一个,窗口管理器可能有另一个,等等。没有标准设备。
Oh, yes, on CLI, the screen program has its own clipboard as well, as do some other applications like Emacsand vi.
哦,是的,在 CLI 上, screen 程序也有自己的剪贴板,其他一些应用程序,如Emacs和vi 也是如此。
In X, you can use xclip.
在 X 中,您可以使用xclip。
You can check this thread for other possible answers: http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-07/0919.html
您可以查看此线程以获取其他可能的答案:http: //unix.derkeiler.com/Newsgroups/comp.unix.shell/2004-07/0919.html
回答by doug
Make sure that you are using alias xclip="xclip -selection c"
otherwise you can't just use to Ctrl+vto paste it back in a different place.
确保您正在使用,alias xclip="xclip -selection c"
否则您不能仅使用 to Ctrl+v将其粘贴回不同的位置。
echo test | xclip
Ctrl+v=== test
Ctrl+v=== test
回答by jtruelove
回答by Norman H
回答by Stenemo
Copy and paste to clipboard in Windows (Cygwin):
在 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 getclip (it can be used instead of Shift+ Ins!) and putclip (echo oaeuoa | putclip.exe to put it into clip) exist.
还存在 getclip(它可以用来代替Shift+ Ins!)和 putclip(echo oaeuoa | putclip.exe 将其放入剪辑)。
回答by Wolfgang Fahl
Here is a ready to use bash script for reading the clipboard which works on multiple platforms. Please edit the script here if you add functionality (e.g. more platforms).
这是一个随时可用的 bash 脚本,用于读取可在多个平台上运行的剪贴板。如果您添加功能(例如更多平台),请在此处编辑脚本。
#!/bin/bash
# WF 2013-10-04
# multi platform clipboard read access
# supports
# Mac OS X
# git shell / Cygwin (Windows)
# Linux (e.g. Ubuntu)
#
# display an error
#
error() {
echo "error: " 1>&2
exit 1
}
#
# getClipboard
#
function getClipboard() {
os=`uname`
case $os in
# git bash (Windows)
MINGW32_NT-6.1)
cat /dev/clipboard;;
# Mac OS X
Darwin*)
pbpaste;;
# Linux
Linux*)
# works only for X clipboard - a check that X is running might be due
xclip -o;;
*)
error "unsupported os $os";;
esac
}
tmp=/tmp/clipboard$$
getClipboard >$tmp
cat $tmp
# comment out for debugging
rm $tmp
回答by jeng
A few Windows programs I wrote years ago. They allow you dump, push, append and print the clipboard. It works like this:
我几年前写的一些 Windows 程序。它们允许您转储、推送、附加和打印剪贴板。它是这样工作的:
dumpclip | perl -pe "s/monkey/chimp/g;" | pushclip
It includes source code: cmd_clip.zip
它包括源代码:cmd_clip.zip
回答by nyitguy
For Mac only:
仅适用于 Mac:
echo "Hello World" | pbcopy
pbpaste
These are located /usr/bin/pbcopy
and /usr/bin/pbpaste
.
这些位于/usr/bin/pbcopy
和/usr/bin/pbpaste
。