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

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

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

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

当前回答

Try

xclip

xclip - command line interface to X selections (clipboard) 

man

其他回答

确保使用别名xclip="xclip -selection c" 否则你将无法使用Ctrl+v进行粘贴。

例子: 执行echo -n test | xclip命令后,Ctrl+v将粘贴test

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

昨天我发现自己遇到了一个问题:“如何在不同的用户会话之间共享剪贴板?”当使用Ctrl + Alt + F7 - Ctrl + Alt + F8切换会话时,实际上,您无法粘贴复制的内容。

我提出了以下快速和肮脏的解决方案,基于一个命名管道。它确实很简陋,但我发现它很实用:

user1@host:~$ mkfifo /tmp/sharedClip

然后在发送终端

user1@host:~$ cat > /tmp/sharedClip

最后,在接收端:

user2@host:~$ cat /tmp/sharedClip

现在,你在第一个终端中输入或粘贴任何东西,并且(在点击返回后),它将立即出现在接收终端中,从那里你可以复制和粘贴你喜欢的任何地方。

当然,这并不是严格地从user1的剪贴板中获取内容,使其在user2的剪贴板中可用,而是需要额外的粘贴和复制单击。

这个函数将测试存在哪些剪贴板并使用它。

要验证复制粘贴到你的shell,然后调用函数clippaste:

clippaste () {
    if [[ $OSTYPE == darwin* ]]
    then
            pbpaste
    elif [[ $OSTYPE == cygwin* ]]
    then
            cat /dev/clipboard
    else
            if command -v xclip &> /dev/null
            then
                    xclip -out -selection clipboard
            elif command -v xsel
            then
                    xsel --clipboard --output
            else
                    print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
                    return 1
            fi
    fi
}

对于mac,你可以使用这个函数,它使用pbcopy和pbpaste,但更简单:

将此添加到你的.bashrc或.zshrc:

clp() {
  if [[ -z "$1" ]]
  then
    # No input - act as paste
    pbpaste;
  else
    # Input exists - act as copy
    echo "$1" | pbcopy;
  fi
}

复制使用clp“Content”,粘贴使用clp