Visual feedback of the exit status of the previous command in bash

Put this in your .bashrc, and the current directory in your PS1 will be printed green if the previous command had exit state 0, red otherwise. No more typing 'echo $?', ' && echo ok', '|| echo failed' etc on the command line.

  1. if [ $(tput colors) -gt 0 ] ; then
  2. RED=$(tput setaf 1)
  3. GREEN=$(tput setaf 2)
  4. RST=$(tput op)
  5. fi
  6.  
  7. bash_prompt_command() {
  8. last_exit=$?
  9. exit_to_color=$RED
  10. [ $last_exit == 0 ] && exit_to_color=$GREEN
  11. }
  12.  
  13. export PROMPT_COMMAND=bash_prompt_command
  14.  
  15. PS1="\u@\h \[\$exit_to_color\]\W\[$RST\] \$"

( Inspired by this post )

Trackback URL for this post:

http://dieter.plaetinck.be/trackback/51
Submitted by Dieter_be on Tue, 10/14/2008 - 21:56. categories [ ]

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Security question, designed to stop automated spam bots