In a terminal in OSX I can pipe output to pbcopy and then go into a web browser and paste it. I tried this in Linux with xcopy but when I switch to the browser it just overwrites the clipboard with with whatever was in it the last time the browser was used. What works like pbcopy in Linux?

up vote 154 down vote accepted

if you have X installed you may try xsel in this way :

alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'

or with xclip :

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

now you can use'em :

echo 'go to my clipboard' | pbcopy

when I don't have X I use GNU Screen functionality to copy between open shells in a session using keyboard

to copy : Ctrl-a -> Esc -> go to wanted position * -> Space (to begin selecting) -> press k to go forward mark text -> Enter

to paste : Ctrl-a + ]

* movements are done with vim like key bindings (j, k, l & m).

  • 1
    Thanks this is exactly the information I wanted. Tho now I wonder if there is a way I could make Screen let me use Emacs commands to select the area I want to copy... – Noah Sussman Dec 31 '11 at 16:58
  • 7
    xsel: Can't open display: (null) : Inappropriate ioctl for device – itsazzad Dec 24 '15 at 10:16
  • I solved the "Can't open display" problem on Windows by installing Xming and setting DISPLAY=:0 – scottgwald Dec 25 '17 at 4:49
  • For tmux users, it's more like Ctrl-b, [ -> go to position -> Space -> select texts -> Enter and paste by Ctrl-b, ] – elquimista Oct 14 at 6:13

Put a script like this called pbcopy in your bin folder:

#!/bin/bash
xclip -i -sel c -f |xclip -i -sel p

This will put STDIN in both your selection buffer and clipboard:

echo Hello world |pbcopy
  • Why would the cat be needed? Won't it just redirect stdin to stdout in this case thus useless cat abuse or am I missing something? – Hultner May 21 '15 at 7:53
  • 2
    You're right it does seem like useless cat abuse :-) – Erik May 25 '15 at 10:44
  • 1
    if you're (like me) wondering about where is the mysterious "useless cat", it has been removed by the edit. NTS: in case of confusing comments, check the history ^^' – yaitloutou Jan 31 '17 at 16:40
  • How do you paste the contents after using this? Just using xclip -o ? Or is there a better way? – n1k31t4 May 4 '17 at 5:23

To expand on the solutions of @Erik and @xpixelz; these two scripts should work on both platforms:

pbcopy:

#!/bin/bash
__IS_MAC=${__IS_MAC:-$(test $(uname -s) == "Darwin" && echo 'true')}
if [ -n "${__IS_MAC}" ]; then
  cat | /usr/bin/pbcopy
else
  # copy to selection buffer AND clipboard
  cat | xclip -i -sel c -f | xclip -i -sel p
fi

pbpaste:

#!/bin/bash
__IS_MAC=${__IS_MAC:-$(test $(uname -s) == "Darwin" && echo 'true')}
if [ -n "${__IS_MAC}" ]; then
  /usr/bin/pbpaste
else
  xclip -selection clipboard -o
fi
  • But why would you even have this script on OS X where it's a part of the native userland? – Hultner May 21 '15 at 8:02
  • * as a reference for similar functionality, * cross-platform dotfiles: github.com/westurner/dotfiles/blob/develop/scripts/pbcopy – Wes Turner May 27 '15 at 17:41
  • Still cat abuse and why not check if xclip exist instead and assign an alias if it does? Or check if the pbpaste binary exist would also be an option. Checking the uname seems like an odd approach to the problem. – Hultner May 29 '15 at 15:19
  • 1
    @Hultner "Checking the uname seems like an odd approach to the problem" You could also check for binaries, preferentially, in order, by exception with a ${a:-${b:-${c:+${d}}}} and some type -P/ has variable assignments and /bin/test execs. – Wes Turner Dec 13 '15 at 20:16

This answer refers to the Linux Subsystem for Windows.

Short answer: use clip.exe as if it were pbcopy in order to put something on the Windows clipboard. It's magic. Example echo "Hello Windows" | clip.exe in your bash or Ubuntu bash terminal, and then `ctrl-v' in a Windows program.

More context:

In a comment above I mentioned that, when using Xming on Windows to enable this functionality, it is necessary to set a DISPLAY variable (export DISPLAY=:0, in many cases) before the xsel and xclip solutions work. Infuriatingly, this solution works in an unreliable, stochastic way -- when pasting from Linux to Windows, pressing ctrl-v between one and ten times causes the clipboard to be pasted (one time) (this is on my Windows 10 Surface Book 2). Don't waste your time, use clip.exe.

NOTE: Don't forget the .exe. Otherwise Ubuntu bash will suggest that you install the Linux package geomview, which is not what you want.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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