smiley in bash prompt

By Jan Veldeman

After the geekdinner of last Tuesday, I couldn’t resist creating my own blog.

As an appetizer, I’ll start with some code snippets I’ve found very usefull. For example the part of my .bashrc which configures the prompt:

smiley() {
    ret_val=$?
    if [ "$ret_val" = "0" ]
    then
        echo ":)"
    else
        echo ":( ($ret_val)"
    fi
}
Green="\033[0;32m"
Yellow="\033[1;33m"
Normal="\033[0m"
PS1="\[$Yellow\]\u@\h\[$Normal\]:\[$Green\]\w \$(smiley) \[$Normal\]"

I always use this because:

  • the coloring makes it easy to see where my previous command started
  • the smiley quickly shows me if and how the previous program exited

Tags: , ,

13 Responses to “smiley in bash prompt”

  1. Peter Vandenabeele Says:

    Jan,

    Welcome to the blog community :-)

  2. Serge van Ginderachter Says:

    Welcome. If you want to be syndicated on planet.geekdiner.be, drop me a line with your rss url and a link to your avatar of choice.

  3. Bart Van Loon Says:

    ma godverdekke seg… hier nog al ene. ;-)

    ‘t amusement ermee!

  4. Smarter BASH prompt | bdeferme's words Says:

    [...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t [...]

  5. Koen Vervloesem Says:

    Interesting idea, but why not using the color to show if your command exited with or without error? An example (inspired by you and Bert Deferme):

    Red=”33[0;31m"
    Green="33[0;32m"
    Yellow="33[1;33m"
    Normal="33[0m"

    correct() {
    ret_val=$?
    if [ "$ret_val" = "0" ]
    then
    echo -e “$Green+”
    else
    echo -e “$Red- ($ret_val)”
    fi
    }
    PS1=”\[$Yellow\]\u@\h\[$Normal\]: \$(correct) \[$Normal\]”

    This gives you a green + if the command goes well and a red – with the error code if something goes wrong.

  6. Jan Veldeman Says:

    Koen: thx for the suggestion!

  7. Paul Cobbaut Says:

    Good start! And useful info ;-)

    cheers,
    paul

  8. Jan Veldeman Says:

    Reading the comments (thank you all!), I improved the scripts a bit.
    Delete sections to your own taste:

    Yellow=”\033[1;33m"
    Blue="\033[0;34m"
    Green="\033[0;32m"
    Red="\033[0;31m"
    LightRed="\033[1;31m"
    Normal="\033[0m"
    when_ok() {
    ret_val=$?
    if [ "$ret_val" = "0" ]
    then
    echo -e “$@”
    fi
    return $ret_val
    }
    when_failed() {
    ret_val=$?
    if [ "$ret_val" != "0" ]
    then
    echo -e “$@”
    fi
    return $ret_val
    }
    smiley() {
    when_ok “:-)”
    when_failed “:-( ($?)”
    }
    PS1=”\$(when_ok ‘\[$Green\]+ ‘)\$(when_failed ‘\[$LightRed\]- ‘)\[$Yellow\]\u@\h\[$Normal\]:\$(when_ok ‘\[$Green\]‘)\$(when_failed ‘\[$LightRed\]‘)\w \$(smiley) \[$Normal\]”
    export PS1

    (too bad these comments aren’t formatted correctly: the quotes are somehow converted and spaces stripped. If someone knows how to correct this, please let me know)
    Note:

    it’s important to escape the when_ok/when_failed so that it’s executed at the correct time
    when_ok/when_failed need to return the result so that the correct return value can be used in the next when_ok/when_failed
    ANSI color codes are explained on wikipedia

  9. Anje Stiers Says:

    Hi Jan,

    thanks for the suggestion. I’m definitely going to use it!
    And of course, I’m glad to see that you ‘ve finally created your own blog.

    Kind regards,
    Anje

  10. bdeferme’s words » Blog Archive » Smarter BASH prompt Says:

    [...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t [...]

  11. noname Says:

    just for the record:

    http://www.thehobbsfamily.net/how-tos/2008/07/28/custom-bash-prompt

    :)

  12. Smarter BASH prompt | bdeferme dot net Says:

    [...] reading this post I tried out the code snippet of Jan but honestly my eyes did not like it. The yellow didn’t look [...]

  13. [t]csh prompt magic « BBBlog Says:

    [...] refer to Jan’s smiley system. I like the idea, and tried to implement something similar in [t]csh… with only limited [...]

Leave a Reply