Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Copy and Paste from the Mac OS X Command Line?

I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes, I'm a tech writer. What I'm wondering is if there's any way to actually copy and past into the Mac system wide copy/paste buffer directly from the command line?


Dave's Answer:

Now this is the kind of question I really dig. And what's great is that it turns out that yes, there is a very simple pair of utilities in Mac OS X that let you monkey with the copy / paste buffer (it's actually called the "Clipboard", though for some reason in the command line version they refer to it as the "pasteboard", which isn't something I've ever seen before in the Unix/Linux world).

I don't know how this would work with multi-clipboard utilities like iClip since I don't use them, but I imagine that it'd simply affect one buffer while leaving all the others alone. If someone has one of these utilities installed, it'd be 90 seconds to test it out...

Anyway, I started my quest with a very useful command: man -k paste, which revealed the following:

Pod::Simple::DumpAsText(3pm) - -- dump Pod-parsing events as text
paste(1) - merge corresponding or subsequent lines of files
pbcopy(1), pbpaste(1) - provide copying and pasting to the pasteboard (the Clipboard) from command line
pboard(8) - pasteboard server
text(ntcl), tk_textCopy(ntcl), tk_textCut(ntcl), tk_textPaste(ntcl) - Create and manipulate text widgets
XStoreBytes(3), XStoreBuffer(3), XFetchBytes(3), XFetchBuffer(3), XRotateBuffers(3) - manipulate cut and paste buffers

If you want to work on the command line, ignore anything from section 3 of the manual: we're looking for "(1)" here, and that shows the commands we seek: pbcopy and pbpaste.

They turn out to be really easy to work with. Want to push the output of an "ls -l" into the Clipboard? Here's how:

$ ls -l | pbcopy

There's no output. So how do you know it worked? Let's just open up a TextEdit window and use Edit --> Paste to see what happen:

mac copy paste command line textedit

Very cool.

To extract something from the Clipboard and have it output within the world of the command line, use pbpaste. Here's a fun command pipe that counts how many letters are in the paste buffer:

$ pbpaste | wc -c
252

Easy, eh? Let's test it...

$ who am i | pbcopy
$ pbpaste | wc -c
32
$ pbpaste
taylor ttys000 Dec 28 17:24
$

So there you go. Turns out to be quite easy to copy and paste between Mac OS X GUI apps and the command line. Now, what to do with it... Hmmm....

Here's one thing I did: I have a script that automatically resizes images for me, spitting out the appropriate HTML for image inclusion in blog posts (like this one). Until I wrote this article and learned more about pbcopy and pbpaste, I'd just manually copy and paste the resultant HTML code. Instead, here's what I did to tweak my 'while condition; do' loop:

while complicated condition do
...lots of code...
done | tee $tempfile
cat $tempfile | pbcopy
rm -f $tempfile

See what's happened here? By using the handy "tee" utility, I still see the output on the screen, but a copy is saved in the tempfile defined by the "tempfile" variable, then sent to the "pbcopy" utility and removed. The result: the output looks exactly the same, but it's auto-copied into the Clipboard. Neat.


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  




Categorized: Mac OS X Help , Shell Script Programming , Unix and Linux Help   (Article 10194, Written by )
Tagged: cli, clipboard, command line, copy paste buffer, linux programming, mac os x, mac programming, scripting, shell script programming
Previous: How do I force a Windows Update in Windows 7?
Next: Remove a favorite from my iPhone "favorites" list?




Reader Comments To Date: 1

kevin v said, on April 24, 2012 8:31 PM:

Thanks for your post. I want to make a funky script to help me use Growly Notes better.

I want to be able to copy the NAME of a file to the clipboard such that when I paste it into Notes- it shows up as an icon- JUST like it does if I use the GUI for the entire thing sequence.

When I right-click and copy a file icon- pbpaste just shows the file path text... but there must be other attributes b/c when I send that same exact text to the clip using pbcopy--- it only pastes the string into Notes instead of the little icon... any ideas?

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.