[Gllug] show non-zero exit code in bash

salsaman salsaman at xs4all.nl
Wed Dec 28 15:09:27 UTC 2005


Russell Howe wrote:

>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...
>
>  
>
Try this then:

PROMPT_COMMAND='[ "x$?" == "x0" ] || echo latest exit code $?;'



Gabriel.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list