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
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
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 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.