[Gllug] show non-zero exit code in bash

Dean Wilson dwilson at unixdaemon.net
Wed Dec 28 19:26:46 UTC 2005


On Wed, Dec 28, 2005 at 06:21:04PM +0000, Tethys wrote:
> 	show_exit_code()
> 	{
> 		retval=$?
> 		cmdnum="$(history 1 | sed 's/\(^[0-9 ]*\).*/\1/')"
> 		if [ $retval -ne 0 -a "$cmdnum" != "$lastcmdnum" ]
> 		then
> 			lastcmdnum="$cmdnum"
> 			echo "Exit code: $retval"
> 		fi
> 	}

show_exit_code() {
  retval=$?
  if [ $retval -ne 0 -a "$HISTCMD" != "$lastcmdnum" ];then
    lastcmdnum="$HISTCMD"
    echo " -- exit code: $retval"
  fi
}

I've tweaked it a bit (and reformatted it to suit my .bash_profile) and
by using $HISTCMD I can drop the two external commands

> This also handles the corner cases:
> - Successive commands with non-zero exit codes
> - "Null commands", i.e., pressing return at the shell prompt

Which is what my initial version failed to deal with.

A pint is yours to claim Mr Tethys!

  Dean
-- 
Dean Wilson             http://www.unixdaemon.net
Profanity is the one language all programmers understand
  --- Anon
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list