linux - [Solved-5 Solutions] Pipe output and capture exit status in Bash - ubuntu - red hat - debian - linux server - linux pc
Linux - Problem :
If you need to execute a long running command in Bash, and both capture its exit status, and tee(command) its output.
Linux - Solution 1:
- There is an internal Bash variable called $PIPESTATUS;
- It’s an array that holds the exit status of each command in your last foreground pipeline of commands.
It works with other shells (like zsh):
Linux - Solution 2:
Using bash's set -o pipefail
"pipefail: the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status"
Linux - Solution 3:
Linux - Solution 4:
There's an array that gives exit status of each command in a pipe.
Linux - Solution 5:
- This solution works without using bash specific features or temporary files.
- In the end the exit status is actually the exit status and not some string in a file
Situation:
It you need the exit status from someprog and the output from filter.
You can use this: