How to pipe command output to the clipboard in OS X
In addition to standard output redirects, Apple supports a couple of alternatives for managing Terminal command output.
When you run commands in the OS X Terminal, they will usually output some result, such as the information you are trying to look up, or status details about the command being run. By default this output is send to the Terminal window, where it is displayed for you to see before the command exits and drops you back to the command prompt. If you are intending to use this output for something you can copy and paste it into another file, but there are other ways to save the information that you may find helpful.
One common approach to saving Terminal output is to redirect it to a file on disk, which can be done by appending to the command a greater-than symbol followed by a path to the file where you would like to save the output.
For instance, the command "ls" will list the current directory's contents. By default this list will display in the Terminal, but if you use the following command it will save the listed items in a file called "files.txt" located on your Desktop:
ls > ~/Desktop/files.txt
If the document "files.txt" already exists, then this command will overwrite it with the output of the "ls" command, but you can use two greater-than symbols ("ls >> ~/Desktop/files.txt") to append new output to the end of the file instead of overwriting. Of course, to look at the output, you'll still need to go to that file and open it.
Another approach is to pipe the results of the command to Apple's "open" command and instruct it to open the file in a text editor like Apple's TextEdit program. While redirecting with the greater-than symbol, as noted above, sends the command's output to a file, piping sends the output to another command, so it can manage the output (parse it, save it, print it, or otherwise act on it).
To pipe the output of a command, you simply use the vertical line character ("|"), such as in the following example, which will take the output of the "ls" command and pipe it to the "open" command, which the "-fe" tag will instruct to read the piped input and open it in TextEdit (the default text editor):
ls | open -fe
When you run this command, instead of the listed output appearing in the Terminal, the Terminal will simply drop to another command prompt, and the output will appear in a plain text window in TextEdit. You can then more easily parse or copy and paste the text, as you can now interact with it using a cursor and arrow keys.
Opening the output in TextEdit this way is convenient, but if you wish to move it to another document such a manuscript you are working on in Microsoft Word or Apple's Pages programs, or if you would like to e-mail the command output to someone, then you will still need to select it and copy it. If this was your intent all along, then you can skip the step of highlighting text by saving the output of the command directly to the OS X clipboard. To do this, pipe the output to the "pbcopy" (pasteboard copy) command, as follows:
ls | pbcopy
With this done, you can now go to any program and paste by pressing Command-V, and the output should be preserved in formatted plain-text.