4

I would like to print at every lauched command in zsh exit code value. E.g.

$ cat file_not_present
cat: file_not_present: No such file or directory
Exit code 1

I only know that I can print error code of last command launched in terminal with

$ echo $?

How can I do?

1

3 Answers 3

10

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.

3
  • I find my own way of printing bold exit code: TRAPERR() print -u2 "Exit status: ${bold}$?${normal}" I set bold and normal bash variable as follow bold=$(tput bold); normal=$(tput sgr0) May 26 at 15:03
  • 2
    @lecciovich95, use print -Pu2 "Exit status: %B%?%b" instead. zsh also has a builtin version of tput: echoti, as well as $terminfo[bold] May 26 at 15:49
  • thank you, it works! It's more compact solution :) May 26 at 16:03
3

You can add any user defined function into precmd_functions:

print_status() echo $?
precmd_functions+=(print_status)

It is executed before every prompt, that means after the command.

0
3

In zsh, you can use the precmd hook and do something like this (taken from this answer):

% PROMPT_COMMAND='printf "Exit code: %s\n" $?'
% precmd() { eval "$PROMPT_COMMAND" }
% cat file_not_present
cat: file_not_present: No such file or directory
Exit code: 1               

In bash, you can use the PROMPT_COMMAND variable for that:

$ PROMPT_COMMAND='printf "Exit code: %s\n" $?'
$ cat file_not_present
cat: file_not_present: No such file or directory
Exit code: 1
2
  • 1
    Also, doing that ugly eval "$PROMPT_COMMAND" would only be for bash users trying to minimise differences when using zsh. Doing it the other way round (use PROMPT_COMMAND=precmd in bash and define precmd() for both) would seem cleaner to me. May 26 at 10:26
  • @StéphaneChazelas sounds reasonable. I don't use zsh myself, I just found that SU answer, checked that it works, and posted.
    – terdon
    May 26 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.