它是可能的管道到/从剪贴板在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?
无论是连接到设备手柄还是使用辅助应用程序,我都找不到任何东西。
例如,如果/dev/clip是一个连接到剪贴板的设备,我们可以这样做:
cat /dev/clip # Dump the contents of the clipboard
cat foo > /dev/clip # Dump the contents of "foo" into the clipboard
当前回答
pbcopy内置在OS X中:
复制.bash_profile文件的内容:
cat ~/.bash_profile | pbcopy
其他回答
下面是一个可以在多个平台上使用的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
如果您像我一样在没有根权限的Linux服务器上运行,并且没有任何xclip或GPM,您可以通过使用临时文件来解决这个问题。例如:
$ echo "Hello, World!" > ~/clip
$ echo `cat ~/clip`
Hello, World!
在Wayland上,xcopy似乎不能工作。使用wl-clipboard代替。
例如,在Fedora上:
sudo dnf install wl-clipboard
tree | wl-copy
wl-paste > file
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
在Windows子系统for Linux (WSL)上,您可以使用clip.exe复制到剪贴板:
cat file | clip.exe
请记住使用|管道命令。而不是>命令,因为这将不起作用。