Did you notice the command error exit in the shell?

28 points by monzool


tomsmeding

My prompt is normally green and turns red if the previous command didn't exit with code 0. (The command typed is in unstyled text, it's just the prompt that's coloured.) This is much less screaming than the version in OP, but also works well. It also has the exit code in there.

By the way, this is default in fish, which I use. :)

acatton

This is built-in zsh with setopt printexitvalue.

It's documented as "PRINT_EXIT_VALUE":

Print the exit value of programs with non-zero exit status. This is only available at the command line in interactive shells.

gerikson

I have a bash function that sets the terminal character "$" to red if the command fails.

Edit here's where I stolecopied it from

https://stackoverflow.com/a/16715681

monzool

What I like about this solution is, that it allow me to bring extra attention for specific programs.

I use starship which changes prompt (🠆) to red on error exit. It is perhaps a bit subtle, but is generally perfectly fine for most cases. I think its just my workflow for git that trips me once in a while. I always use the command line for git commands. I will typically switch to the multi-tab never-closed Wezterm terminal; fire the commands and then toggle back to vscode. So the git error message is there in the terminal. The current prompt arrow is red. Heck, even the old branch name listed by starship is still there (although that is not always easy to distinguish). Its lack of attention from my part in that specific workflow

wink
λ(user@host ~) $ 
-1-(user@host ~) $

the error part is red and not white like the (fluff) λ . Good enough even at a glance, been running this or a variation of it for probably 15 years.

bkhl

I do something similar but do it like this, so that the highlighted error code is part of the PS1: https://github.com/bkhl/dotfiles/blob/main/.bashrc#L155

Actually modifying the prompt string rather than printing directly to the terminal avoids some input issues that can otherwise happen.

mordae

I just print in red: "Error: ${code}" after every failing top level command. I don't do this for pipefails.

Tainting the prompt with previous command status never felt right to me.

I've had it like this since I've built LFS in 2004 or so. LFS teaches you hard to watch out for failing commands I guess.

EDIT: here's the code:

function err() { echo -e "\e[1;31mError: \e[33m$?\e[0m" >&2; }
trap err ERR
jrwren

i love the amiga throwback. well done@

reezer

grml zsh config which I use on every system/server/OS shows me the return code in red, if it wasn't 0.

fanf

My shell prompt looks like

fanf@basalt.dotat.at:~/Code/ops/wwwdotat (trunk)
: 0 ; 

The username / hostname / working directory is in scp syntax. (I used to deal with a lot of computers and often switched user so this is vital for situational awareness.) The git branch is a relatively recent addition (this century); it breaks triple-click to select the whole path, but in practice I don’t need to do that very often. The line break allows the path to grow without pushing the command line into the right margin. The :; is a no-op command that allows me to triple-click copy and paste the command line. The 0 is the exit status. I don’t use colour in my prompt, it’s boldface which is just enough to stand out from command output.

patryk

I've got the following snippet in my .zshrc and it does the job without more than 1 LOC and doesn't bork the layout of the regular prompt (which makes it a tad easier to miss, but I'm fine with that)

RPROMPT='%F{red}%(?..%?)%f'

This shows exit code only if it was non-zero (as much as I don't like info-pages, zsh info is great)

chrismorgan

I prefer to integrate the exit code itself into the prompt:

~$ true
~$ false
~ [1]$  foo
zsh: command not found: foo
~ [127]$ 

Coloured red, it’s plenty visible enough for me.

Adapted from the end of my zsh $PROMPT, after the working directory part

%(?,, %F{red}[%?]%f)$ 

Read through “EXPANSION OF PROMPT SEQUENCES” of man zshall for explanation of all % bits. %(?,,
) is a ternary on exit status, %? is the exit status itself. I don’t actually use %F myself, but instead write the escape sequence out manually, from habit, including literal ␛ (Vim: Ctrl-V Escape) rather than \e, though this way then requires %{
%} wrapping to get cursor positioning right:

%(?,, %{\e[31m%}[%?]%{\e[m%})$
Sanity

mine turns the $ into a red 1 (or whatever the non-zero exit code was)