pbcopy and pbpaste

May 5, 2009

Here’s a quick tip about two handy command line utilities for Mac OS X: pbcopy and pbpaste.

pbcopy is used to capture output from any command line program and place it into the pasteboard. An example:

$ cat hello.txt 
hello world!
$ cat hello.txt | pbcopy 

We can now paste the output from cat in the regular cmd-v fashion mostly anywhere.

pbpaste, on the other hand, outputs the contents of the pasteboard. An example:

$ pbpaste 
hello world!

In this way, pbcopy and pbpaste can be used to move data between command line programs and GUI applications.

pbcopy and pbpaste have a few tricks up their proverbial sleeves. You can read all about them in their man page, which is available online, or simply by entering man pbcopy on the command line.

Update: Boy genius Mattias Arrelid pointed out that to get pbcopy and pbpaste to play nicely with UTF-8, you need to set __CF_USER_TEXT_ENCODING correctly. Read the full scoop over at Mac OS X Hints.