how to get the exit status of the first command in a pipe?
bash, exit-code, linux, shell
Solution
Use the `PIPESTATUS` array:
$ ls foo | cat
ls: foo: No such file or directory
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
2 0
Note: `PIPESTATUS` is a bashism (i.e. not POSIX).
Problem
I made a simple script: ``` $ more test.bash #!/bin/bash echo test exit 1 ``` When I run the script , the exit status should be 1 ``` $ /tmp/test.bash echo $? 1 ``` But when I run this as the following ``` /tmp/test.bash | tr -d '\r' 1>>$LOG 2>>$LOG echo $? 0 ``` The exit status is 0, (not as expected 1) It seems that the exit status comes from tr command. But I what I want is to get the exit status from the script - test.bash. What do I need to add/change in my syntax in order to get the right exit status from the script, and not from the command after the pipe line?