the second in this series of obscure Linux features that wizards should know, introduced Standard I/O. This month we’ll see how to take advantage of Standard I/O from a shell– including an example using named pipes (FIFOs).

" />
x
Loading
 Loading
Hello, Guest | Login | Register

Wizard Boot Camp, Part Three: Using Standard I/O from Shells

Last month’s column, the second in this series of obscure Linux features that wizards should know, introduced Standard I/O. This month we’ll see how to take advantage of Standard I/O from a shell– including an example using named pipes (FIFOs).

Last month’s column, the second in this series of obscure Linux features that wizards should know, introduced Standard I/O. This month we’ll see how to take advantage of Standard I/O from a shell– including an example using named pipes (FIFOs).

Duplicating a file descriptor

The Bourne shells’ operator m>&n copies a file descriptor. The first digit, m, is the file descriptor you want to change. The second digit, n, is the file descriptor to copy.

For instance, 2>&1 copies f.d. 2 from f.d. 1. This makes f.d. 2 point to the same open file as f.d. 1 does. Because f.d. 2 is the standard error and f.d. 1 is standard output, after using that operator, stderr will point to the same place as stdout.

The operator 2>&1 isn’t any use in the default situation where both stderr and stdout point to the terminal. One place it is handy is when you’re piping the output of one command to another– for instance, when you’re viewing the output of a program with a pager program like less (1). Without the 2>&1 operator, like this:

 $ grep xyz * | less 

Only the stdout of grep is piped to less. grep‘ s stderr goes to the terminal– along with the stdout of less. So, if grep outputs an error, it’s likely to be mixed into the output text from less; you may not notice the error at all. Using 2>&1 fixes that:

 $ grep xyz * 2>&1...

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Power Up Linux GUI Apps
  2. Tweeting from the Command Line with Twyt
  3. When Memory Serves You: Using ramfs and tmpfs
  4. Disk-O-Tech: Linux Disk Management
  5. Bang for the Buck
Follow Linux Magazine
Rackspace