Is there functionality in Unix that allows for the following:
echo "Some Text" | copy-to-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
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
On Mac OS X there are the lovely pbcopy and pbpaste commands which are very helpful :)
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)
or:
printf %s $(pwd) | xclip -selection clipboard
The "" around $(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.
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).