I want to check wether the output of my command contains "rerun" (and then rerun) but I also want to display the whole output. I know that I could use one of these:

command | tee >(grep rerun)
command | grep rerun

The first one prints the whole output as expected, but I can't use it as a condition because it always returns 0. The second one only prints the lines that contain rerun, but it returns 1 if there's no match.

My usage example:

while pdflatex paper.tex | grep -E "rerun LaTeX|run Biber"; do
    biber paper

The answers provided here also don't help because there grep always returns 0.

  • I also tried command | tee >(echo) | grep rerun but that doesn't give any output as well. – Max Matti Aug 2 '18 at 12:54
  • So you want the whole output, the matching lines or something else? I can't get the reason for such manipulations. – Kondybas Aug 2 '18 at 13:49
  • Yes I want the whole output, as stated in the question. There is also a reason given, by giving an exact usecase. – Max Matti Aug 3 '18 at 11:43

Just use

command | tee outfile | grep rerun


while pdflatex paper.tex | tee outfile | grep -E "rerun LaTeX|run Biber"; do

You can check the status of the grep command, and you can later look at the file "output".


you could tee to a file and perform the grep on the file. Then you can use the grep exit code (0 when there's a match):

while [[ $RERUN == 1 ]] ; do
  biber paper
  ! pdflatex paper.tex | tee output.txt && grep -E -q "rerun LaTeX|run Biber" output.txt

The ! on the 4th line inverses the exit code of the grep process because grep returns 0 when it finds a match and 1 when no match, see the grep man page:


   Normally the exit status is 0 if a line is selected, 1 if no lines were
   selected, and 2 if an error occurred.  However, if the -q or --quiet or
   --silent is used and a line is selected, the exit status is 0  even  if
   an error occurred.

5th line puts the last exit code ($?) in the RERUN var which is used in the loop condition.

I also added the -q option to grep to not write to stdout

  • What does the ! at the beginning of the fourth line do? Does it negate the output of grep? – Max Matti Aug 3 '18 at 11:46
  • @MaxMatti sorry for the late response, i was on vacation. yes, indeed, I updated the answer to answer your comment. Does it work now? – mx1up Aug 16 '18 at 15:31

If we stick with displaying stdout right away then the grep output in Process Substitution should be redirected to a file lest you get duplicated lines in the output, so you have to do for example this: command | tee >(grep rerun >/tmp/my.log).

Once you have a file produced, all you have to do is to use [[ -s /tmp/my.log ]] as your rerun condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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