Copy Shell Output via the Command Line

By  on  
Oftentimes I'll want to copy the output of a shell command execution but can't get the whole output because it's larger than the terminal's buffer length, so I'll need to write to file for easy viewing, or I'll simply be annoyed that I have to click-hold-drag to copy the output.  Shouldn't there be an easier way?  There is:  pbcopy and clip.  Using pbcopy within the shell, the output of an execution can automatically be added to the copy queue (or clipboard):
# Copy the source of davidwalsh.name to the clipboard on Mac
curl davidwalsh.name | pbcopy

# Copy the source of davidwalsh.name to the clipboard on Windows
curl davidwalsh.name | clip
Piping pbcopy at the end of the command makes this magic possible.  So what do I look forward to using this for?  My colleague Luke showed me how he gets the commit hash from the master branch without needing to go to GitHub to get it:
alias ghash='git rev-parse HEAD && git rev-parse HEAD | pbcopy'
I look forward to using pbcopy more -- an excellent utility to allow me to avoid lame cursor click-hold-drag to get the output I want!

Recent Features

  • By
    From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!

    My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Incredible Demos

  • By
    HTML5’s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

  • By
    Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

Discussion

  1. MaxArt

    It may be silly to notice that this isn’t for Windows…

  2. Might be worth adding instructions using xcopy on Linux, and clip on Windows

  3. I am on Linux and I use http://www.vergenet.net/~conrad/software/xsel/

    Use it like this:
    curl davidwalsh.name | xsel –clipboard –input

    You can also alias is so you can use pbcopy

    To do that add alias pbcopy=’xsel –clipboard –input’ to your ~/:bashrc

  4. suprsidr

    windows would be curl davidwalsh.name | clip
    assuming you had curl for windows installed ;)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!