Pipe to/from the clipboard in Bash script

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

You're a little ambiguous. I expect you're probably a Linux user inside X who wants to put stuff in the X PRIMARY clipboard.

It's important to understand that bash doesn't have a clipboard. There is no such thing as "the" clipboard, because bash can run on Windows, Mac OS X, lots of other OSes, inside X, outside X, ... Not to mention that X itself has three different clipboards. There's a wealth of clipboards you could be dealing with. Usually the clipboard you want to talk to has a utility that lets you talk to it.

In case of X, yes, there's xclip (and others). xclip -selection c will send data to the clipboard that works with Ctrl-C, Ctrl-V in most applications.

If you're trying to talk to the Mac OS X clipboard, there's pbcopy .

If you're in Linux terminal mode (no X) then maybe you need to look into gpm .

There's also GNU screen which has a clipboard. To put stuff in there, look at the screen command " readreg ".

Under Windows/cygwin, use /dev/clipboard or clip for newer versions of Windows (at least Windows 10).


Make sure you are using alias xclip="xclip -selection c" otherwise you can't just use to Ctrl+v to paste it back in a different place.

echo test | xclip    

Ctrl+v === test


Install

# You can install xclip using `apt-get`
apt-get install xclip

# or `pacman`
pacman -S xclip

# or `dnf`
dnf install xclip

If you do not have access to apt-get nor pacman , nor dnf , the sources are available on sourceforge.

Set-up

Bash

In ~/.bash_aliases , add:

alias setclip="xclip -selection c"
alias getclip="xclip -selection c -o"

Do not forget to load your new configuration using . ~/.bash_aliases . ~/.bash_aliases or by restarting your profile.

Fish

In ~/.config/fish/config.fish , add:

abbr setclip "xclip -selection c"
abbr getclip "xclip -selection c -o"

Do not forget to restart your fish instance by restarting your terminal for changes to apply.

Usage

You can now use setclip and getclip , eg:

$ echo foo | setclip
$ getclip
foo
链接地址: http://www.djcxy.com/p/46940.html

上一篇: 如何将许多PDF文件合并成一个文件?

下一篇: 在Bash脚本中输入/输出剪贴板