I tried to edit my bash prompt to fit my needs and provide a clean working environment. Here is what I gathered so far, picking ideas here and there for what I found useful.

My bash prompt

Functionalities

This prompt collapses the current path directories names and display only the first letter of each name, except the last directory which is printed in its entierety.

The color of the user name depends on the success of the previously executed command: green if its return status was 0 (SUCCESS!!), red otherwise.

The prompt is compatible with Python’s virtualenv and shows the current virtualenv name.

The actual input prompt starts on the beginning of the next line with a $ when run as a normal user or with a # when run as root.

How to use it

Nothing complicated here, just copy and paste the following snippet at the end of your ~/.bashrc file

1
2
3
4
5
6
7
8
myprompt() {
    STATUS_COLOR='$([ $? != 0 ] && printf "\[\033[1;31m\]" || printf "\[\033[1;32m\]")';
    COLLAPSED_PATH='\[\033[1;34m\]$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")';
    PROMPT_SYMBOL='$([ `id -u` -eq 0 ] && printf "\[\033[0;33m\]#" || printf "\[\033[0;37m\]$")';
    VIRTUAL_PROMPT=${VIRTUAL_ENV:+"\[\033[0m\](`basename $VIRTUAL_ENV`) "}
    export PS1="$STATUS_COLOR\u@\h $VIRTUAL_PROMPT$COLLAPSED_PATH \n$PROMPT_SYMBOL\[\033[0m\] ";
}
PROMPT_COMMAND="myprompt"

Go further

From here, you are free to edit this prompt and customize it to fit your own needs.

I might add details about the git repository I’m browsing such as the branch name, whether its is clean or files were modified…