53

Is there functionality in Unix that allows for the following:

echo "Some Text" | copy-to-clipboard
3
  • you must mean stdout?
    – phunehehe
    Nov 10, 2010 at 0:36
  • 4
    it really depends on which side of the pipe your standing on :P to the command that sends it to the clipboard, its stdin.
    – Stefan
    Nov 10, 2010 at 4:53
  • 3
    People who have used Unix for a long time will tell you it's better to write <longTextFile straightToClipboard. It's the same as cat longTextFile straightToClipboard, but doesn't require running cat. 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, 2011 at 10:23

6 Answers 6

42

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
5
  • 5
    Another one is xclip; it accepts from stdin by default. I only use it because it's the first such tool that I learned of. Nov 9, 2010 at 19:04
  • @Shawn Post it as a separate answer :) Nov 9, 2010 at 19:35
  • I think -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
  • 1
    xsel didn't work for me (I don't know why), but xclip (per other answers) did.
    – markrian
    Jan 17, 2017 at 15:28
  • @MichaelMrozek There is neither a xsel nor a xclip on RedHat/CentOS7. Do you know of another tool available on CentOS7 ?
    – SebMa
    Jul 19 at 12:11
15

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
1
  • 4
    Not trying to revive an old question, but if you're lazy then echo "Some Text" | xclip -sel c works too.
    – anonymoose
    Jan 28, 2019 at 20:39
9

you can use xsel

xsel < file 
4
  • 12
    The question is: Which clipboard? Linux X server has 3 (generally, only 2 are used)... xsel uses the PRIMARY clipboard by default.. The PRIMARY clipboard kicks in automatically every time you simply select soemthing. You paste if by pressing the center mouse button.. The Ctrl+C / Crtr+V type clipboard is called the CLIPBOARD clipboard :).. so if you want to use the Ctrl+C / Ctrl+V clipboard with 'xsel', the command is: 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, 2011 at 11:42
  • gnome-terminal. Edited. @jamespo - this doesn't seem to work.
    – ripper234
    Apr 5, 2011 at 12:26
  • works for me on gnome-terminal in ubuntu 10.04 using the xsel in the repo (paste with middle button)
    – jamespo
    Apr 5, 2011 at 15:42
  • This should have been a comment to Michael Mrozek's answer. Oct 30, 2020 at 9:14
9

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

2

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.

0

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

1
  • 2
    xclip requires the -selection clipboard option. The default selection per its man page is something else.
    – Asclepius
    Oct 9, 2014 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.