Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/robzbd/sites/blog.ubrio.us/wp-content/plugins/akismet/widget.php on line 126

Warning: ob_start(): non-static method wpGoogleAnalytics::get_links() should not be called statically in /home/robzbd/sites/blog.ubrio.us/wp-content/plugins/wp-google-analytics/wp-google-analytics.php on line 288
Best Bash Prompt

Best Bash Prompt

Okay — so after that last post I decided it was time for a change. I’ve had the same bash prompt for years and was starting to get tired of it. Over the past 30 or so minutes I’ve been hating life learning way more than I wanted to about PS1, colors and embedded IF statements.

And yes, this needs it own post simply for the fact that this is an insane prompt. Heres what it is/can do:

  1. Automatically shortens itself if the path is longer than 18 charachters
    This is what I’ve always wanted since I usually name my folders after the website URL — and I HATE 2 line prompts.
  2. Changes to red if the last command didn’t have a return code of 0
  3. Includes your bash history # for easy history command repeating
  4. Is pretty neat and trim
Why is this rad?

Prompt Madness!

I’ll let this image explain what it does and how it looks…

I’m tossing up a text file here -> best_bash_prompt_ever.txt — since the escaping will probably be messed up.

export PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"'
The second half (from “echo -ne…” on) is for the Terminal’s title.


  1. Robbie March 3rd

    Comment Arrow

    very cool ;) , i would like to change it a bit though and swop it around so that the command numbers get changed [001]=green for good , [002]red for bad. do you know how i could do that? i still like my prompt as” [user@comp] ~ $ ” although i’ve now added ” [829][user@comp] ~ $ ” nice work though ;) I’ve been inspired :)

  2. Jadu Saikia December 28th

    Comment Arrow

    cool play with PS1, nice info, good tip. Thanks.

    // jadu

  3. Talon June 23rd

    Comment Arrow

    That person above is a complete newb if he doesnt realise that files chmoded to 0000 show as white. ! :) :/ :/

  4. Nasko July 27th

    Comment Arrow

    function elite

    local GRAY=”\[33[1;30m\]”
    local LIGHT_GRAY=”\[33[0;37m\]”
    local CYAN=”\[33[0;36m\]”
    local LIGHT_CYAN=”\[33[1;36m\]”
    local YELLOW=”\[33[1;33m\]”
    local NO_COLOUR=”\[33[0m\]”

    case $TERM in
    local TITLEBAR=’\[33]0;\u@\h:\w07\]’
    local TITLEBAR=”"

    local temp=$(tty)
    local GRAD1=${temp:5}
    $CYAN\$(date +%H)$YELLOW:$CYAN\$(date +%M)$YELLOW:$CYAN\$(date +%S)$GRAY/$CYAN\
    $(date +%d-%b-%y)\

  5. desa78 May 3rd

    Comment Arrow

    Unfortunately your prompt cannot handle directory names that contain a parenthesis… any solution?

  6. desa78 May 4th

    Comment Arrow

    I realized that the prompt will not work for directory names that contain parentheses. Is there any solution?

  7. desa78 May 4th

    Comment Arrow

    improved version:

    export PROMPT_COMMAND=’PS1=”\[33[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo “\\[\33[32m\\]“; else echo “\\[\33[31m\\]“; fi\`[\u.\h: $(if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo “\\W”; else echo “\\w”; fi)]\$\[33[0m\] “; echo -ne “33]0;`hostname -s`:`pwd`07″‘

Add Yours

  • Author Avatar


Comment Arrow

About Author

Rob Hurring

Ruby, Rails, PHP, bash... oh my!