3

The following bash pipeline returns 1:

$ false | true
$ echo $?
1

However it returns 0 when executed in an script:

$ cat test.sh
#!/usr/bin/env bash
false | true
echo $?

$ bash test.sh
0

Can someone please explain why?

  • 1
    Run set -o | grep pipefail in an interactive shell and in a script. – Satō Katsura Sep 4 '17 at 16:36
5

The standard behavior for bash is to return exit status of the last command in the pipeline, as in your script. It looks that you have enabled pipefail option in the interactive shell which forces the return of the last command with non-zero exit status. Here is how it works:

$ set -o pipefail    # enable pipefail
$ false | true
$ echo $?
1

$ set +o pipefail    # disable pipefail
$ false | true
$ echo $?
0

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.