[Gllug] show non-zero exit code in bash

Russell Howe rhowe at siksai.co.uk
Wed Dec 28 14:19:47 UTC 2005


On Sat, Dec 24, 2005 at 11:43:20AM +0000, Dean Wilson wrote:
> On Fri, Dec 23, 2005 at 03:07:01PM +0000, Russell Howe wrote:
> > > I have shell envy. One of my co-workers uses zsh and has a nifty feature
> > > that shows you the exit code of any command that doesn't return a 0.
> 
> > You need to track state. Something like this:
> > PROMPT_COMMAND='ret=$?; if [ "x$SHOWNRETCODE" != "xyes" -a "$ret" -ne "0" ];then echo exit code: $ret; SHOWNRETCODE=yes; fi'
> 
> That's closer but it's still not right. The problem with that one is
> that it only shows an error code once. No matter which error code comes
> afterwards.

Ugh, well I'd had a bit of wine at that point... :)

You could use this:

PROMPT_COMMAND='newret=$?;if [ "x$ret" != "x$newret" ]; then ret=$newret; [ "x$ret" == "x0" ] || echo latest exit code $ret; fi'

Although it will miss the case where you run two programs in succession
which return the same non-zero result code...

With zsh, I'd probably try and include it in RPROMPT, that way it
doesn't steal any vertical screen space...

-- 
Russell Howe       | Why be just another cog in the machine,
rhowe at siksai.co.uk | when you can be the spanner in the works?
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list