My bash prompt

If you are working a lot on the Linux command line like I do, you probably want to have a nice, fancy looking bash prompt that shows you some more information than the default one. Today, I am going to show you the prompt I am currently using. I got the ideas for it from different sources all over the internet, but I decided to implement it (and comment it!) on my own. If you want to give it a try, just copy the following lines to the .bashrc file in your home directory:

function update_prompt {
    # get information to be displayed in prompt
    hostname=$(hostname | tr -d '\n')
    username=$(whoami | tr -d '\n')
    working_directory=$(pwd | tr -d '\n' | sed "s:^$HOME:~:")
    date_time=$(date "+%H:%M")

    # compute size of prompt an number of fill characters
    local terminal_width=${COLUMNS}
    local promptsize=$(echo -n "--( $working_directory )--( $username @ $hostname )--" | wc -c)
    local fillsize=$(($terminal_width-$promptsize))


    # check if we have to truncate the current working directory
    if [ "$fillsize" -lt "0" ]
        # working directory is too long to be fully displayed
        # -> cut off leading characters
        local cut_position=$((3-$fillsize))
        local length_working_directory=$(echo -n "$working_directory" | wc -c)
        # working directory is short enough to be fully displayed
        # -> create enough fill characters to align working directory to the right
        local fill_characters=""
        while [ "$fillsize" -gt "0" ]

    local col_none="\[\033[0m\]"
    local col_yellow="\[\033[1;33m\]"
    local col_red="\[\033[0;31m\]"
    local col_green="\[\033[0;32m\]"

    local col_light_blue="\[\033[1;34m\]"
    local col_light_gray="\[\033[1;37m\]"
    local col_light_purple="\[\033[1;35m\]"
    local col_light_green="\[\033[1;32m\]"
    local col_light_turquois="\[\033[1;36m\]"

    PS1="$col_yellow--( $col_light_turquois$working_directory$col_yellow )-${fill}-( $col_red$username $col_yellow@ $col_light_purple$hostname$col_yellow )--\n$col_yellow--( $col_green$date_time$col_yellow )--> $col_none"


As you might have noticed, this is a two-line prompt where the first line scales to fit the width of the terminal. It tells you the current working directory, the username and hostname, and the current time. The cool thing is that the working directory is truncated if it is too long to be fully displayed. This is how it looks like:

Note how the working directory is truncated in the second line.