Shell pipe: Exit immediately when one command fails

前端 未结 5 873
长发绾君心
长发绾君心 2020-12-28 15:09

I\'m using a pipe of several commands in bash. Is there a way of configuring bash to terminate all commands in the whole pipeline immediately should one of the commands fail

5条回答
  •  醉梦人生
    2020-12-28 15:32

    find / |false fails faster because the first write(2) system call from find fails with the error EPIPE (Broken pipe). This is because false has been already terminated and hence the pipe between these two commands has been closed already on one side.

    If find would ignore that error (it could do so in theory) it would by also "fail slow".

    (sleep 5 && echo "Hello") | false is "fail slow", because the first part, sleep, does not "test" the pipe by writing to it. After 5 seconds the echo also get the EPIPE error. Whether this error terminates the first part in this case or not is not important to the question.

提交回复
热议问题