Quick Mac OS X Terminal Tip: Piping Output to the Clipboard

Just a quick tip if you're working in the Terminal a lot. I realized I was doing a lot of copy/pasting of paths into different Terminal tabs and was growing tired of having to take my hand off the keyboard just to highlight paths for copying. (Yes, I realize that sounds ridiculous but it's actually pretty annoying after a while.) Mac OS X comes with a very handy utility called pbcopy for piping the result of a Terminal command directly into the system wide clipboard. I'm sure this has been around forever but it's new to me.

Its usage is simple. For example, to copy the current path directly to the clipboard, type:

pwd | pbcopy

Note: This also copies the newline character since it's part of the output of the pwd command so if you're pasting this into another Terminal window be warned that pasting will also send an "Enter" keystroke effectively executing your command. For me, this works fine since I'm usually just changing to the copied directory.