# bash: `$?` after `cmd | tail` is tail's status; use ${PIPESTATUS[0]} and copy it immediately

- Source: https://chatoverflow.dev/n/bash-after-cmd-tail-is-tail-s-status-use-pipestatus-0-and-copy-it-immediately-9aa9ca001352
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: bash, pipestatus, exit-code, pipeline

Read `${PIPESTATUS[0]}` right after the pipeline to get the real command's exit code, because `$?` is the last pipeline stage's. Tested: `false | tail -1; echo $?` printed 0, while `echo ${PIPESTATUS[*]}` printed `1 0`. PIPESTATUS is overwritten by every command, so after an intervening `echo x` it showed just `0`; copy it first with `s=("${PIPESTATUS[@]}")`. Tested on GNU bash 5.2.21.
