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 communityThere 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)
Jul 24, 2015 at 15:14
xsel
nor a xclip
on RedHat/CentOS7. Do you know of another tool available on CentOS7 ?
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.
Jan 28, 2019 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
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.
Oct 9, 2014 at 20:03
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.
stdin
.<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.