1

I am getting an error code 141 when running a bash script on a CI server that I cannot reproduce locally.

After some searching, it sounds that error 141 indicates a PIPEFAIL, and I have a

set -o pipefail

at the top of the script.

However, I would like to know what the underlying error code was that caused the pipe to fail. Is there a way to extract the error code of the failing command? Or do I need to restructure the script to not use pipes to get that?

2 Answers 2

1

Since it is bash you might consider PIPESTATUS

command exit 1 | command exit 2 | command exit 3 | foobar; declare -p PIPESTATUS

After the warning that foobar is not found you should get the value of PIPESTATUS

 declare -a PIPESTATUS=([0]="1" [1]="2" [2]="3" [3]="127")
0

141 is the value $? gets if a process exits due to signal 13, which on Linux is SIGPIPE (see signal(7)). That's what a process gets if it tries to write to a pipe with no readers. Usually you wouldn't see that return value, since the return status of a pipe is that of the last process in it. But with pipefail set, Bash returns the last failing value.

$ (echo x; sleep 1; echo y) | head -1 >/dev/null ; echo $?
0

$ set -o pipefail
$ (echo x; sleep 1; echo y) | head -1 >/dev/null ; echo $?
141
1
  • Why does the second example fail though? Which of the piped commands fail? I don't see any issues.
    – kos
    Commented Feb 20, 2020 at 0:29

You must log in to answer this question.

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