Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I just wondered why you can't grep on some --help commands.

For example if I use ifconfig --help | grep unix it still prints the whole help instead of the line with unix.

But on grep --help | grep null it works fine.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

ifconfig seems to print its help to stderr instead of stdout. Since piping only passes stdout to the next program, grep does not receive the output.

An easy way around that is redirecting stderr to stdout. Here's an example on how to do that with ifconfig's help.

ifconfig --help 2>&1 | grep unix

produces the wanted output

unix (UNIX Domain) inet (DARPA Internet) inet6 (IPv6)

Update: 2>&1 is what actually redirects (>) stderr (2) to the same output (&) as stdout (1). Since usually both stdout and stderr get printed to your terminal, you don't see a difference, but for piping, the actual stream is important.

share|improve this answer
    
Thats interessting. Tank you. But why do some applications write there help to stderr? And what does 2>&1 mean? –  0xAffe Nov 28 '13 at 10:22
    
Probably because their coders figured stdout is for actual output of the program, not hints on how to use it. As for the 2>&1, see update. Also, if an answer solved your problem, please accept it, so other can use it to solve similar issues. –  drc Nov 28 '13 at 11:10
1  
@Loris1123: it's good to separate errors from normal output: that way you can inform about what happens, and that output (on stderr) is NOT taken as the output from the command (imagine a script that decide if grep something ce_fichier ; then do THIS ; else do THAT ; fi : if ce_fichier is not existant, grep can tell so on STDERR (if it did on STDOUT the "if" would see lines, and therefore believe that "something" was indeed in "ce_fichier" !). And adding '--help' to any command will output the help, so it better output this part on stderr... –  Olivier Dulac Nov 28 '13 at 13:20

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.