How do I send stdout to the clipboard?
Is there functionality in Unix that allows for the following:
echo "Some Text" | copy-to-clipboard
There are a couple tools capable of writing to the clipboard; I use
xsel. It takes flags to write to the primary X selection (
-p), secondary selection (
-s), or clipboard (
-b). Passing it
-i will tell it to read from stdin, so you want:
$ echo "Some Text" | xsel -i -b
you can use xsel
xsel < file
On Mac OS X there are the lovely
pbpaste commands which are very helpful 🙂
The simplest is probably
$ echo "Some Text" | xclip
Then paste using your mouse’s middle button.
xsel, it is usually not installed by default, so you might need to install it (
sudo apt-get install xclip on Debian/Ubuntu).
xclip, as @Nicolas suggested, if you want to later paste the contents of the clipboard, such as using Ctrl+V, you can use it this way:
$ echo "Some Text" | xclip -selection clipboard
xclip is a good way to go as answered by @Nicolas Raoul but when piping anything containing a newline to the clipboard, such as
pwd, the newline is also copied. In some situations it may be desired, but mostly one doesn’t want the newline.
The solution is either:
echo -n $(pwd) | xclip -selection clipboard
-n removes the newline from the echoed argument)
printf %s $(pwd) | xclip -selection clipboard
$(pwd) may be required but it works with and without on ubuntu with bash.
In Wayland, this can be done with wl-clipboard:
$ echo "hello" | wl-copy
Similarly, the clipboard can be returned as follows:
$ wl-paste hello
Cygwin (and hence also MSYS2 and Git Bash) has the /dev/clipboard device for accessing the Windows clipboard, so input/output can simply be redirected there.