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 1 year 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

      Consolidating Lotus Domino x86 Workloads on IBM Power Systems

      Read the white paper to learn how moving up to Lotus Domino 8.5 and consolidating with IBM Power Servers can help you boost performance results and ROI.

      White Paper

      A Comparison of PowerVM and VMware vSphere (4.1 & 5.0) Virtualization Performance

      This technical white paper presents benchmark results showing greater VM consolidation ratios than demonstrated in previous benchmarks and demonstrating the extent of the performance lead that PowerVM virtualization technologies deliver over x86-based add-on virtualization products.

      White Paper

      Task, workflow & issue management for teams. Try free!

      Need a flexible system for managing team tasks, issue tracking, and automating and managing workflow processes? Comindware® Tracker helps you do it all.

      White Paper

      How Nimsoft Service Desk Speeds Deployment and Time to Value

      For years, many support teams have been hamstrung by their traditional service desk platforms, which require complex, time-consuming coding for virtually every aspect of customization. This complexity makes it costly and difficult for support organizations to adapt-and places an increasingly substantial burden on the agility and efficiency of the business as a whole.

      Webcast On Demand

      Best Practices in Monitoring VMware

      The benefits of virtualization are unassailable: increased agility, scale, and cost savings to name a few. However, so too are the monitoring challenges posed by these environments-including complexities, lack of visibility and control, and inefficiency.

      Sponsor: Nimsoft

      See more White Papers | Webcasts

      Ask a question

      Ask a Question