Skip to Content

how do I pipe STDERR?

Pronco's picture

How do I pipe STDERR?

I do not want to redirect STDERR or STDOUT to a file. I want to 'pipe' std error through a filter (grep)

How do I grep the STDERR output of a commond without saving it to a file?

search in vain? Does bash/bash2 not have this ability? Is this ability simply not indexed on google? What search engine can find my answer?

Maybe /dev/null will help me in the current process

Conceptor's picture

File descriptors

this is making a custom file descriptor with value three . this override the bash/shell default file descriptors. and save the current stdout value you can make it 4 6..9 .do not do 5 .

exec 3>&1 

execute your command and pipe it to grep

command 2>&1 >&3 3>&- | grep word  3>&-  
  1. 1 2 3 4 5 6 7 8
  1. your command
  2. redirect the output at the default stdout
  3. this will call the the saved value which u make for it save
  4. close file descriptor 3 for 'grep'
  5. the world famous pipe
  6. grep #man grep for details
  7. your needed words
  8. close output file descriptor 3.

close the upper exec 3>&1 to resume work on your shell as defaults :

exec 3>&-  

Diaa Radwan

Pronco's picture

stderr must be one of the wor

stderr must be one of the worlds worst inventions..

2>&1 1> /dev/null

That redirects stderr to stdout and stdout to /dev/null.

foo 2>&1 | grep "whatever"

BTw, conceptor yours is appreciated and thank you for your plentiful information


I used to be indecisive .. but now I'm not so sure

For future refernce this is a

For future refernce this is a simpler way to do it (worked for me) command 2>$1 | grep word

(essentially the same but skips the extra fd by first redirecting stderr directly to stdout and then pipe it all to grep)

See http://www.teaser.fr/~ama

See http://www.teaser.fr/~amajorel/stderr/

Essentialy, you need to know this command: foo >out 2>&1

which means "take stdout, put it in a file called out, and take stderr and put it at the same place stdout goes".

Pronco's picture

Down

link could not be found


I used to be indecisive .. but now I'm not so sure

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Dr. Radut | forum