Hi Dave. I’ve been learning more about the command line on my Mac system thanks to your Learning Unix for OS X book. Fun! I’m curious, though, is there a way to access the copy / paste buffer from within Terminal?
While you may think that the Mac system, with its highly polished graphical interface, would be the last place to find a sophisticated command line interface, you’d be wrong! No, it’s not 1999 all over again, but Apple is well aware that with its Unix/Linux underpinnings, MacOS X has its fair share of hackers, programmers and power users who all enjoy mucking about on the command line as much as they do using that slick, 3D user interface.
This extends to the relationship between the Terminal, the command line and the greater MacOS X system too. Though not really well documented, there are a lot of interesting commands that enable some pretty cool scripts and commands. One of my favorites is open. Check “man open” to see how it can open up apps and/or open up documents in the correct graphical app directly from the command line. Cool.
In terms of the copy/paste buffer, yes, you can interact with it directly from the command line using the commands pbcopy and pbpaste. As a simple example:
$ uptime | pbcopy
You can then go into Pages, your graphics editor or even in a Web browser and post it as part of a Web post:
9:05 up 11 days, 18:48, 2 users, load averages: 1.08 1.52 1.73
The pbcopy man pages references multiple pasteboards (clipboards, whatever you want to call the copy/paste buffer) but I only use the default, as shown.
This offers up some interesting possibilities even without leaving the Terminal app itself. For example, when I want to build an alias to let me easily log in to a remote computer, I’ll pre-load the correct password into the copy/paste buffer within the alias, so I can log in with a simple Cmd-V:
alias synth='echo '\''MyPassWord123'\'' | pbcopy; sftp secretadmin@backend.askdavetaylor.com'
See how that works? echo | pbcopy is a very useful sequence for scripts if you want to preload content!
The other half of the dynamic duo is pbpaste and that’s interesting too because you can grab content anywhere – including on this very Web page! – and paste it into your command line sequence, even in the middle of a sequence of commands.
For example, let’s create a command that lets you easily grab the TITLE attribute of whatever Web page URL is currently in the copy/paste buffer! This is pretty easy to do, actually, with the powerful curl command:
curl -s $(pbpaste) | grep -i '<title>'
I’ll run it with https://www.webmd.com/ in the buffer:
$ curl -s $(pbpaste) | grep -i '<title>' <title>WebMD - Better information. Better health.</title>
See how that works? You can even reference pbpaste in a subshell (the $( ) sequence) and have it work.
Now, armed with both pbcopy to move content into the buffer and pbpaste to pull it back out, go forth and do cool stuff!
Pro Tip: I’ve been writing about the command line forever. Check out my extensive Linux help pages while you’re here!