I looked at Exiting a pipeline if a former command fails but I'm looking for a more general solution to this problem:

chlist -q -v -m -r $sourcelist | \
grep '^USER:' | \
chlist -q -v -f - $superlist

the problem is if the first chlist fails than no input into the 2nd chlist is very bad indeed.

Is there any solution to stopping the 2nd chlist from running other than putting the output of the first one into a temp file and then checking the exit status (eg not using a pipeline at all)?

3 Answers 3


All commands in a pipeline are started asynchronously, so if you want to ensure that the last command doesn't run if there is no input you need to split up the pipeline as you suggest. Alternatively maybe chlist (which I don't know of) has an option to do that for you.


After messing around with this some more, I came up with the idea below and I think it might be a workable and pretty general solution:

( rm -f FAIL ; chlist -q -v -m -r $sourcelist || touch FAIL ) | \

( test -e FAIL || chlist -q -v -f - $superlist )

(I omitted the grep for clarity here, but this works fine with it in there)

Another, (cleaner?) variation:

( chlist -q -v -m -r $sourcelist || touch FAIL ) | \

( rm FAIL 2>/dev/null || chlist -q -v -f - $superlist )
  • This may work sometimes by bad luck, but definitely not in general. The pieces of a pipeline are run concurrently, and if your hardware supports it simultaneously, NOT sequentially. Try (sleep 1; touch foo) | ls foo; ls foo Dec 4, 2018 at 12:03
  • Do not the short-circuit rules of || still apply? Dec 4, 2018 at 12:42
  • Short-circuit is conditional but not concurrent; a||b must wait for a to complete before deciding whether to execute b. Try ((sleep 5;false)||touch foo)|(ls foo;date +D%F.%T);stat -cS%y foo and see the timestamp on foo (labelled S) is 5 seconds after the time displayed by date (labelled D) Dec 6, 2018 at 16:42

How about appending a dummy line to the first command's output that satisfies the input requirements of the last one but doesn't trigger an action?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .