How do I send stdout to the clipboard?

Is there functionality in Unix that allows for the following:

echo "Some Text" | copy-to-clipboard
Asked By: Stefan


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
Answered By: Michael Mrozek

you can use xsel

xsel < file 
Answered By: jamespo

On Mac OS X there are the lovely pbcopy and pbpaste commands which are very helpful 🙂

Answered By: cwd

The simplest is probably xclip:

$ echo "Some Text" | xclip

Then paste using your mouse’s middle button.

Like xsel, it is usually not installed by default, so you might need to install it (sudo apt-get install xclip on Debian/Ubuntu).

Answered By: Nicolas Raoul

Using 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
Answered By: danidemi

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

(the -n removes the newline from the echoed argument)


printf %s $(pwd) | xclip -selection clipboard

The "" around $(pwd) may be required but it works with and without on ubuntu with bash.

Answered By: calocedrus

In Wayland, this can be done with wl-clipboard:

$ echo "hello" | wl-copy

Similarly, the clipboard can be returned as follows:

$ wl-paste

Answered By: ishigoya

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.

Answered By: ak2
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.