Is there functionality in Unix that allows for the following:
echo "Some Text" | copy-to-clipboard
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIs 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
-i is not required in this case: man xsel 1.2.0 says: and the selection is set from standard input if standard input is not a terminal (tty)
– Ciro Santilli新疆棉花TRUMP BAN BAD
Jul 24 '15 at 15:14
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
echo "Some Text" | xclip -sel c works too.
– anonymoose
Jan 28 '19 at 20:39
you can use xsel
xsel < file
xsel -ib <file-long-or-short ("Look ma, no cat!" :) ... or if you like <file-long-or-short xsel -ib .. -i is default. -b is for the Ctrl+C/Ctrl+V type of clipBoard
– Peter.O
Apr 5 '11 at 11:42
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.
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).
xclip requires the -selection clipboard option. The default selection per its man page is something else.
– Acumenus
Oct 9 '14 at 20:03
stdin. – Stefan Nov 10 '10 at 4:53<longTextFile straightToClipboard. It's the same ascat longTextFile straightToClipboard, but doesn't require runningcat. Just an observation. Feel free to ignore it. See The Useless Use of Cat Award for some background and examples if you're interested. – Mikel Apr 5 '11 at 10:23