Capture output of certain Unix commands

By Rob Griffiths, Macworld.com |  Operating Systems, Mac OS X, Unix 1 comment

Today's hint will probably only appeal to those of you learning to use the Unix side of OS X. A while back, I was trying to capture the output of the Unix command httpd -t (which runs a syntax check on the Apache web server's configuration files) to the clipboard. Typically, you do this in OS X by sending the command's output through the pbcopy (pasteboard copy) command:

httpd -t | pbcopy

That should, in theory, put the output of the command on the clipboard. But in this case, it wasn't working. After much investigation, what I learned is that some Unix commands, httpd -t included, don't send their output to the Terminal in the usual way. The usual way in Unix is to send output to something called the "standard output," which for all intents in this discussion, means the Terminal window.

Instead, certain commands send their output to standard error (STDERROR, in Unix parlance). Although this text is also displayed in Terminal, you can't do anything with the output as you can if it were sent to standard output. (Unix wizards, please feel free to correct any of my mistakes in the above; it's meant to be a summary view of the situation.)

The solution is to reroute what gets sent to standard error to standard output, using this syntax:

httpd -t 2>&1

The 2 represents standard error, and the 1 represents standard output. The bit in the middle, >& is the usual Unix redirect command (the greater than sign), followed by the ampersand, which changes the redirect's output from a file (which is typical) to a file descriptor (in this case, the standard output).

I know that's a mouthful, and I'll admit that I don't fully understand it--but I know it works, at least in the bash shell that's been the default for a while now. Once the output is in standard output, it can just be fed through pbcopy as usual:

httpd -t 2>&1 | pbcopy

The end result, assuming there's nothing wrong with your Apache configuration files, will be the text Syntax OK on your clipboard. I know that ssh -v (verbose mode ssh) also sends its output to standard error, as does time, and I'm sure there are many more.

If you're a Unix expert and can clarify or expand on this in the comments, please feel free!

See more like this: unix-linux, Terminal, Mac OS

1 comment

    Anonymous 45 weeks ago
    Actually, the '&' goes with the '1' to specify that the target is standard output (&1). You could just a easily redirect the standard error to a different file with'2 > /somepath/somefile'.This does not change your solution however, because the pipeline '

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      Operating SystemsWhite Papers & Webcasts

      White Paper

      Microsoft Enterprise Agreement Program Overview

      Discover how flexible the Microsoft Enterprise Agreement Program is to help you build the right software solution agreement for your business. This paper highlights all the available options-from on-premise software and cloud service solutions, to payment options and enrollment programs, and more.

      White Paper

      Watson - A System Designed for Answers. The future of workload optimized systems design

      Watson is a workload optimized system designed for complex analytics, made possible by integrating massively parallel POWER7 processors and DeepQA technology. Read the white paper about Watson's workload optimized system design.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question