Pipe to/from the clipboard in Bash script

2019-01-03 19:08发布

Is it possible to pipe to/from the clipboard in 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:

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

23条回答
手持菜刀,她持情操
2楼-- · 2019-01-03 19:46

There is also xclip-copyfile.

查看更多
【Aperson】
3楼-- · 2019-01-03 19:48

Wow, I can't believe how many answers there are for this question. I can't say I've tried them all but I've tried the top 3 or 4 and none of them work for me. What did work for me was an answer located in one of the comment written by a user called doug. Since I found it so helpful, I decided to restate in an answer.

Install xcopy utility and when you're in the Terminal, input:

Copy

Thing_you_want_to_copy|xclip -selection c

Paste

myvariable=$(xclip -selection clipboard -o)

I noticed alot of answers recommended pbpaste and pbcopy. If you're into those utilities but for some reason they are not available on your repo, you can always make an alias for the xcopy commands and call them pbpaste and pbcopy.

alias pbcopy="xclip -selection c" 
alias pbpaste="xclip -selection clipboard -o" 

So then it would look like this:

Thing_you_want_to_copy|pbcopy
myvariable=$(pbpaste)
查看更多
Deceive 欺骗
4楼-- · 2019-01-03 19:51

Try

xclip

xclip - command line interface to X selections (clipboard) 

man

查看更多
我命由我不由天
5楼-- · 2019-01-03 19:51

in macOS use pbpaste

eg:

update the clipboard

pbpaste | ruby -ne ' puts "\|" + $_.split( )[1..4].join("\|") ' | pbcopy

enjoy.

查看更多
虎瘦雄心在
6楼-- · 2019-01-03 19:52

From this thread, there is an option which does not require installing any gclip/xclip/xsel third-party software.

A perl script (since perl is usually always installed)

use Win32::Clipboard;
print Win32::Clipboard::GetText();
查看更多
登录 后发表回答