Copy the output of a command to clipboard for easy pasting with Command-V in OSX

echo "Example command output..." | tee >(pbcopy)

February 28, 2015Elkku

Explanation

When you want to copy-paste something from a terminal window, selecting with a mouse can be difficult with larger outputs, it's easier to pipe directly to the clipboard.

pbcopy takes the standard input and places it on a specified clipboard or the default. We could pass to it the output of a command using a pipe:

echo ... | pbcopy

The one-liner also makes the output visible, using tee and Process Substitution. tee takes the standard input, prints it to standard output and also saves it in a file. The process substitution syntax >(pbcopy) produces a temporary file (/dev/fd/N), which we pass to tee as a command line argument as the "file" to write to.

As a result, we can see the result of the command on the terminal, and at the same it's saved on the clipboard, so that we can paste with Command-V.

Limitations

This is Mac OSX specific. Use xsel on Linux.