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

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

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

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

当前回答

下面是一个可以在多个平台上使用的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" 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 the 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

其他回答

  xsel -b

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

复制和粘贴到剪贴板在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.

还有getclip(它可以代替Shift + Ins!)和putclip (echo oeuoa | putclip.exe将其放入clip)存在。

2018的答案

使用clipboard-cli。它适用于macOS, Windows, Linux, OpenBSD, FreeBSD和Android,没有任何实际问题。

安装方法:

npm install -g clipboard-cli

然后你可以这样做:

echo foo | clipboard 

如果你愿意,你可以通过在你的.bashrc, .bash_profile或.zshrc中放入以下文件来别名cb:

alias cb=clipboard

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

例如,如果你跑步

cat ~/.bashrc | pbcopy

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

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

pbpaste > my_clipboard.txt

我找到了一个很好的参考:如何瞄准多个选择与xclip

在我的情况下,我想粘贴内容在剪贴板上,也看到什么被粘贴在那里,所以我也使用tee命令与文件描述符:

echo "just a test" | tee >(xclip -i -selection clipboard)

>()是进程替换的一种形式。Bash用连接到括号内程序的标准输入的文件描述符的路径替换它们。

tecommand将您的命令分叉,允许您“管道其内容”并在标准输出“stdout”上查看结果。

你也可以创建别名来获取和写在剪贴板上,允许你使用“pbcopy”和“pbpaste”,就像你在Mac上一样。在我的情况下,当我使用Z shell (zsh)时,我在我的别名文件中有这个:

(( $+commands[xclip] )) && {
    alias pbpaste='xclip -i -selection clipboard -o'
    alias pbcopy='xclip -selection clipboard'
}

Z shell中的(($+命令[name]))测试命令“name”是否安装在您的系统上,然后两个别名都用{}分组。&&是一个二进制AND;如果a,那么b,因此如果你有xclip,那么别名将被设置。

echo "another test" | tee >(pbcopy)

要获取剪贴板内容,只需输入:

pbpaste | "any-command-you-need-here"