Return Code: last command
Without PIPESTATUS
: Only the last return code of piped sequence is checked.
In a piped sequence of commands, $?
will always reflect the exit status of the last command in the pipeline.
Example where we succeed but should fail
success() {
echo.dim "success" >&2
return 0
}
not_success() {
echo.dim "not success" >&2
return 1
}
main() {
echo hi | not_success | success
if [ "$?" -eq 0 ]; then
echo.green "Success (in main)"
else
echo.yellow "Not success (in main)"
return 1;
fi
}
main "${@}" || exit 1
Output
not success
success
Success (in main)