它是可能的管道到/从剪贴板在Bash?

无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。

例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:

cat /dev/clip        # Dump the contents of the clipboard
cat foo > /dev/clip  # Dump the contents of "foo" into the clipboard

当前回答

这是一个简单的Python脚本,可以满足你的需要:

#!/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())

将其保存为路径中的可执行文件(我将其保存到/usr/local/bin/clip.)你可以输入要保存到剪贴板上的内容。

echo "Hello World" | clip

你可以将剪贴板中的内容传输到其他程序……

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

单独运行它只会输出剪贴板中的内容。

其他回答

一种从剪贴板粘贴到文件的方法,不需要任何工具,除了echo。

转义你想要粘贴的文本中的单引号:用“\”替换所有出现的“\”并将结果复制到剪贴板。 输入echo -n ' 按Shift +插入 输入' > filename.txt 按回车键

基本上你是这样做的:

Echo -n复制“text”带“\”单引号“\”转义“> filename.txt”

即使复制的文本有新的行,它也能工作。

  xsel -b

完成X Window的工作,而且它大部分已经安装好了。 看一下xsel的手册页是值得的。

在macOS系统下,请使用内置的pbcopy和pbpaste命令。

例如,如果你跑步

cat ~/.bashrc | pbcopy

~/的内容。可以使用Cmd + V快捷方式粘贴bashrc文件。

要保存当前剪贴板到一个文件,将输出pbpaste重定向到一个文件:

pbpaste > my_clipboard.txt

在Windows上(使用Cygwin)尝试 Cat /dev/clipboard或echo "foo" > /dev/clipboard如本文所述。

xsel在Debian/Ubuntu/Mint

# append to clipboard:
cat 'the file with content' | xsel -ab

# or type in the happy face :) and ...
echo 'the happy face :) and content' | xsel -ib

# show clipboard
xsel -ob

# Get more info:
man xsel

安装

sudo apt-get install xsel