Blog.phpwnage  
Blog.phpwnage > My New Bash Prompt A blog by Kevin Lange, UIUC Class of 2013
  My New Bash Prompt Feb 4, 2010 - klange  
  Last night, I set up a new bash prompt. It's a modified version of my old prompt with a bit more information added:
forum image

In a normal prompt, it's pretty much the same as my old one: user, host, date, time. I added a seconds display, moved the path into the brackets, and then added a new line. When in a git repo, I can see the current branch name, whether I have modified files lying around, and whether I have yet to push (if I'm ahead of the current branch). I'm thinking of expanding this to subversion and Bazaar, both of which I use quite a bit. When a program returns a value other than 0, I also display that after the current time, in bright red. If I'm root, the prompt symbol changes from a green $ to a red #.

The more interesting things to note are why I have so much information there by default. I ssh to a lot of different machines and sometimes into different users, so it's good to be able to identify what user I currently am (whether it be `klange` on my personal systems, `lange7` on a university system, or any of the random users I `sudo --` into on my server). For the same reason, I always need to know immediately what machine I'm connected to, but I don't need the full hostname (I know what machines are part of the ACM network and which are part of EWS, and which are mine, based on naming schemes). I use the clock because my panel is on auto-hide, so the quickest way to glance at the time is to look at the prompt, rather than having to move my mouse to show my panel. It's also good to see when a command ended.

Code:
function prompt_command {
    local RETURN_CODE="$?"
    local ASCII_RESET="\[\e[0m\]"
    local ASCII_BOLD="\[\e[1m\]"
    local USER_COLOR="\[\e[1;33m\]"
    local PROMPT_COLOR="\[\e[1;32m\]"
    if [[ ${EUID} == 0 ]] ; then
        PROMPT_COLOR="\[\e[1;31m\]"
    fi
    local HOST_COLOR="\[\e[1;32m\]"
    local DATE_COLOR="\[\e[1;31m\]"
    local TIME_COLOR="\[\e[1;34m\]"
    local DATE_STRING="\$(date +%m/%d)"
    local TIME_STRING="\$(date +%H:%M:%S)"
    local CYAN_COLOR="\[\e[1;36m\]"
    local PINK_COLOR="\[\e[1;35m\]"
    
    local PROMPT_PREFIX="$PROMPT_COLOR"
    if [[ $RETURN_CODE != 0 ]] ; then
        PROMPT_PREFIX="$DATE_COLOR$RETURN_CODE$ASCII_RESET " # do nothing
    fi
    local GIT_STATUS=`git status 2>/dev/null`
    if [[ $GIT_STATUS != "" ]] ; then
        local REFS=$(git symbolic-ref HEAD 2>/dev/null)
        REFS="${REFS#refs/heads/}"
        if [[ `echo $GIT_STATUS | grep "modified:"` != "" ]] ; then
            REFS="$REFS$ASCII_RESET ${PINK_COLOR}modified"
        fi
        if [[ `echo $GIT_STATUS | grep "ahead of"` != "" ]] ; then
            REFS="$REFS$ASCII_RESET ${CYAN_COLOR}not pushed"
        fi 
        PROMPT_PREFIX="$PROMPT_PREFIX$USER_COLOR$REFS$ASCII_RESET "
    fi
    
    
    PS1="$ASCII_BOLD[$USER_COLOR\u $HOST_COLOR\h $DATE_COLOR$DATE_STRING $TIME_COLOR$TIME_STRING $PROMPT_PREFIX$ASCII_RESET\w$ASCII_BOLD]$ASCII_RESET\n$PROMPT_COLOR\\\$$ASCII_RESET "
}
export PROMPT_COMMAND=prompt_command



Discuss this news post here.
(No comments)
 
  Comment on this Article  
 
klange Read this article here
(More Comments)
 
(C) 2010 Kevin Lange [ RSS ][ Mobile ][ PHPwnage ][ PHP 5 ]