Personally, I add %(?..%B(%?%)%b)
to my $PROMPT
, so that the exit status of the previous command be shown in bold in brackets if it was unsuccessful, which is less intrusive than printing the exit status of every command.
To print the exit status of every failing command, you can do:
TRAPERR() print -u2 Exit status: $?
$ false; false; (exit 123)
Exit status: 1
Exit status: 1
Exit status: 123
(123)$
(the (123)$
being from that $PROMPT
mentioned above).
For every command:
TRAPDEBUG() print -u2 Exit status: $?
But that will likely get very annoying.
You can also print the exit status of the last command that was run with:
print_last_status() print -u2 Exit status: $?
precmd_functions+=(print_last_status)
$ false; (exit 123); true
Exit status: 0
Like for the $PROMPT
approach, you only see the status of the last command run in the command line you sent at the previous prompt.
In any case, printing it on stderr (as I do above with -u2
) will likely cause fewer problems than doing it on stdout. Doing print Exit status: $? > /dev/tty
may be even better.