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
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    Animated AJAX Record Deletion Using jQuery

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with jQuery JavaScript. The PHP - Content & Header The following snippet goes at the...

  • By
    Telephone Link Protocol

    We've always been able to create links with protocols other than the usual HTTP, like mailto, skype, irc ,and more;  they're an excellent convenience to visitors.  With mobile phone browsers having become infinitely more usable, we can now extend that convenience to phone numbers: The tel...

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!