First off stderr and stdin and stdout, whats that? They are file handles / file descriptors. Any program display stuff on the screen thats stdout and stderr. Errors and help messages get put on the screen using stderr. When you do the | command have you noticed it only pipes out stdout. What if you wanted

Good article:
Good cheatsheet:

For example

service –status-all Has alot of output, and trying to pipe for example to see if apache is running service –status-all | egrep “apache” will not show any out. Why? Because all ofservice –status-all output is in stderr. So we need to redirect stderr out to the pipe and then to grep… But how?


THESE PIPE ONLY stderr thru the pipe:

command 2>&1 > /dev/null | command2


command 2>&1 | command 2

OR – this pipes stdout and stdin thru the pipe:

command |& command

COMMON EXAMPLE – for service all, helps (helps are outputed as stderr lots of the time):

rsync –help 2>&1 > /dev/null | egrep -i “log”

rsync –help |& egrep -i “log”
service –status-all |& egrep -i “apache”



Im copy pasting the important stuff here… Taking no credit for it, its all by the wonderful people at stackoverflow – where you questions get answered

New BASH and other shells:

Bash 4 has this feature:

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.

zsh also has this feature.

With other/older shells, just enter this explicitly as

FirstCommand 2>&1 | OtherCommand
You can also Swap process:

( proc1 3>&1 1>&2- 2>&3- ) | proc2
Provided stdout and stderr both pointed to the same place at the start, this will give you what you need.

What it does:

creates a new file handle 3 which is set to the current 1 (original stdout) just to save it somewhere.

sets stdout to got to the current file handle 2 (original stderr) then closes 2.

sets stderr to got to the current file handle 3 (original stdout) then closes 3.

It’s effectively the swap command you see in sorting:
temp = value1;
value1 = value2;
value2 = temp;

Leave a Reply

Your email address will not be published. Required fields are marked *