Small Mosaic


Categories:

/books
/career
/codinghorrors
/events
/geekstuff
/justdont
/languages
/languages/bash
/linkshot
/magazines
/meta
/misctech
/movies
/nottech
/operatingsystems
/operatingsystems/linux
/operatingsystems/linux/debian
/operatingsystems/solaris
/perl
/presentations
/programming
/python
/ruby
/security
/security/apache
/security/tools
/serversmells
/services
/services/dns
/sites
/specifications
/sysadmin
/testing
/tools
/tools/commandline
/tools/firefox
/tools/gui
/tools/network
/tools/online
/tools/online/greasemonkey
/tools/puppet
/unixdaemon

Archives:

July 20111
June 20112
May 20113
April 20112
March 20117
January 20111
December 20103
November 20103
August 20101
July 20101
June 20104
May 20102
April 20101
March 20108
February 20101
January 20102
Full Archives

Thu, 02 Feb 2006

Command Return Codes and Long Command Prompts
Once you've been using a tool for a while you often reach a plateau where it's "good enough" and you stop looking for ways to tweak it. I've been using bash for a number of years and I've got set in my ways; until I sat next to a co-worker who uses zsh.

My first Linux machine had a 14" monitor that could only do low resolutions. Screen space was at a premium and every character was precious. These days most of the machines I spend a lot of time on have 19-21 inch monitors. Unfortunately, I still have the same bash prompt.

As screen estate isn't quite so precious anymore I've changed to a two line prompt:

dwilson@fully.qualified.machine.name:/full/path/to/cwd/
$ type_commands_here

This gives me all the details of where I am (with each part allowing double click selection, ready for pasting in another terminal) while also having an almost empty line ready for my commands. While it still feels a little strange, after eight years of using a single line prompt, it's growing on me.

The other little bash tweak I've added recently, and this was inspired by zsh, is to show all non-zero return codes from commands I've run in the shell. As most of you know, a majority of unix command line tools return '0' on success. If it returns a different exit code then something is probably wrong. While you can check the exit code explicitly with an 'echo $?' this soon becomes very, very tedious. After mentioning what I wanted to do, a number of sample command lines and scripts bounced around the GLLUG list. After some discussion Tethys came up with this little bit of magic:

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

export PROMPT_COMMAND=show_exit_code

If you add this to your .bash_profile (or .bashrc if you source that) every time a command returns a non-zero code the shell will show it to you. This snippet also handles a number of the edge cases, it doesn't get in the way of piping, if a command returns a bad value and you press enter or Ctrl-C the code isn't displayed again and some other annoying bits my initial version didn't deal with.

Like this post? - Digg Me! | Add to del.icio.us! | reddit this!

Posted: 2006/02/02 23:28 | /tools/commandline | Permanent link to this entry | This entry and same date


books career codinghorrors events geekstuff justdont magazines meta misctech movies nottech operatingsystems/linux operatingsystems/linux/debian operatingsystems/solaris perl programming python ruby security security/apache security/tools serversmells services/dns sites sysadmin testing tools tools/commandline tools/firefox tools/gui tools/network tools/online tools/online/greasemonkey tools/puppet unixdaemon

Copyright © 2000-2010 Dean Wilson XML feed logo