58

Is there functionality in Unix that allows for the following:

echo "Some Text" | copy-to-clipboard
Piotr Dobrogost
  • 4,116
  • 5
  • 35
  • 46
Stefan
  • 24,830
  • 40
  • 98
  • 126

8 Answers8

45

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
Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
18

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
Asclepius
  • 376
  • 3
  • 9
danidemi
  • 1,553
  • 2
  • 11
  • 8
  • 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 '19 at 20:39
9

you can use xsel

xsel < file 
jamespo
  • 1,171
  • 7
  • 6
  • 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 – Peter.O Apr 05 '11 at 11:42
  • gnome-terminal. Edited. @jamespo - this doesn't seem to work. – ripper234 Apr 05 '11 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 05 '11 at 15:42
  • This should have been a comment to Michael Mrozek's answer. – Piotr Dobrogost Oct 30 '20 at 09:14
9

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

cwd
  • 44,479
  • 71
  • 146
  • 167
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.

calocedrus
  • 145
  • 6
0

In Wayland, this can be done with wl-clipboard:

$ echo "hello" | wl-copy

Similarly, the clipboard can be returned as follows:

$ wl-paste
hello

ishigoya
  • 151
  • 3
0

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.

ak2
  • 12,395
  • 1
  • 18
  • 14
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).

Nicolas Raoul
  • 7,945
  • 14
  • 43
  • 55
  • 2
    `xclip` requires the `-selection clipboard` option. The default selection per its man page is something else. – Asclepius Oct 09 '14 at 20:03