Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

According to "Linux: The Complete Reference 6th Edition" (pg. 44), you can pipe only STDERR using the |& redirection symbols.

I've written a pretty simple script to test this:

#!/bin/bash
echo "Normal Text."
echo "Error Text." >&2

I run this script like this:

./script.sh |& sed 's:^:\t:'

Presumably, only the lines printed to STDERR will be indented. However, it doesn't actually work like this, as I see:

    Normal Text.
    Error Text. 

What am I doing wrong here?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

I don't know what text your book uses, but the bash manual is clear (if you're a little familiar with redirections already):

If |& is used, the standard error of command1 is connected to command2's standard input through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the standard error is performed after any redirections specified by the command.

So if you don't want to mix standard output and standard error, you'll have to redirect standard output somewhere else. See How to grep standard error stream (stderr)?

{ ./script.sh 2>&1 >&3 | sed 's:^:\t:'; } 3>&1
share|improve this answer
add comment

|& pipes stderr to stdin, like 2>&1 |, so the next program will get both on stdin.

$cat test.sh
#!/bin/bash
echo "Normal Text."
echo "Error Text." >&2
$./test.sh | sed 's:^:\t:'
Error Text.
        Normal Text.
$ ./test.sh |& sed 's:^:\t:'
        Normal Text.
        Error Text.
share|improve this answer
    
Oh, so it's basically equivalent to the longer expression runcommand 2>&1 | tee? ie runcommand |& tee? –  Naftuli Tzvi Kay Nov 10 '11 at 23:52
    
yes, they're the same. –  Kevin Nov 11 '11 at 3:36
add comment

|& in bash is just a (not terribly portable) shortcut for 2>&1 |, so you should see every line indented.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.