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?
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