Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use iTerm2 as my terminal client in Mac OS X. On the local system I can use pbcopy and pbpaste to transfer data between the system clipboard and the terminal, but of course this doesn't work when you're ssh'ed to another machine.

Is there some way which I can take the result of a command and copy it to the clipboard automatically? Perhaps an applescript to grab the text on the iTerm windows, then get the next to last line?

For instance, if I wanted to copy the current working directory:


I run pwd, then use the mouse to select the text, and then press command + c.

Is there any better / faster / automatic way of doing this? I'm not looking for a bulletproof solution that would work for every command (eg: might not work when there is a huge scrollback) - I'm just looking for something to make this task that I do quite often a little less tedious.


I'm looking into using screen to do this, but I'm still not sure if it is possible.

share|improve this question
up vote 2 down vote accepted

Well, I just tried this and it works:

echo "foo" | tee | ssh pbcopy


  • I'm using echo "foo" as a stand-in for the command whose output you want to copy to your ssh client machine.
  • I'm using tee so you can see it right in your terminal window instead of having it all swallowed up by the ssh command.
  • By giving ssh a command to run, it will send the stdin that ssh received to that command on that other host, and then immediately return.

You could probably alias it to something simpler to type. And be sure to use ssh keys instead of password-based authentication to save yourself having to retype your password. Update: And you can use SSH Agent Forwarding so you don't have to put credentials to access your local machine on the remote box.

(NB: I'm not sure how well pbcopy/pbpaste work when you only have an ssh/tty/shell session and no Mac OS X GUI context. I think the pasteboard is a NeXTStep/Cocoa/Aqua/GUI concept, not necessarily something that exits at the Unix layer without the GUI layer.)

When I first came up with this it seemed hackish, but the more I play around with it the more I like it.

share|improve this answer
This does look like one way to accomplish this (+1), but I don't really like the idea of having credentials to access my local machine stored on a remote box that I'm working on. That's why I was thinking of doing it more as a screen-grab. Can you think of any other ways to accomplish this? – cwd Mar 28 '12 at 16:07
@cwd You can use SSH Agent Forwarding so you don't have to put credentials to access your local machine on the remote box. – Spiff Mar 28 '12 at 16:52
the article describe the concept OK but I guess I still need to figure out the commands to make that happen. Also I found this: which looks promising – cwd Mar 28 '12 at 17:23

Simplistic solution

On your ssh create /usr/local/bin/spbcopy:

cat >$HOME/.pastebin

On you OSX create /usr/local/bin/spbpaste:

ssh $* 'cat .pastebin; >$_'

Copy data to clipboard using:$ echo "foo" | spbcopy

And paste data from clipboard using:

osx$ spbpaste

You can probably use named pipe on remote host and make the data copied to local clipboard automatically by running some command in background, but it makes it just more complex to setup.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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