[Gllug] show non-zero exit code in bash

Greater London Linux User Group gllug at gllug.org.uk
Mon Dec 26 23:08:20 UTC 2005


Dean Wilson <dwilson at unixdaemon.net> writes:

> 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.

I don't think you'll achieve this by setting anything to do with the
prompt. $? stays untouched until another command is executed, ignoring,
quite rightly IMO, commands run by bash itself.

What you essentially want to do is follow each command with:
     || echo "Oops! Buggered by a '$?'."

(Of course you could make it more elegant by creating an alias, say 'e',
so you could simply say '||e')

Not quite as simple as merely changing the prompt but there may be a way
to do this, or something similar, automatically. You would need to find
some way to run code after command completion.

You could always hack the bash source to add this functionality if it's
not already there :)

Matthew

-- 
I must take issue with the term "a mere child," for it has been my
invariable experience that the company of a mere child is infinitely
preferable to that of a mere adult.
                                           --  Fran Lebowitz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 194 bytes
Desc: not available
URL: <http://mailman.lug.org.uk/pipermail/gllug/attachments/20051226/071ea74d/attachment.pgp>
-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list