Rev. Johnny Healey's Blog

Just another WordPress.com weblog

with one comment

Here’s a little snippet that you can stick into your .bashrc file. It creates a hash of the hostname and uses that to color the ps1 variable, so that hosts with similar names look different when you are shelled into them.

function colorps1() {
    word=`hostname --short`
    hash=`echo "$word" | md5sum`

    fullstr=""
    for l in `echo $word | sed 's/\(...\)/\1\n/g'`; do
        control=""
        endcontrol='\[33[00m\]'
        case "${hash:0:1}" in
            0)
            control='\[33[1;30m\]'
            ;;
            1)
            control='\[33[0;31m\]'
            ;;
            2)
            control='\[33[0;32m\]'
            ;;
            3)
            control='\[33[0;33m\]'
            ;;
            4)
            control='\[33[0;34m\]'
            ;;
            5)
            control='\[33[0;35m\]'
            ;;
            6)
            control='\[33[0;36m\]'
            ;;
            7)
            control='\[33[0;37m\]'
            ;;
            8)
            control='\[33[1;30m\]'
            ;;
            9)
            control='\[33[1;31m\]'
            ;;
            a)
            control='\[33[1;32m\]'
            ;;
            b)
            control='\[33[1;33m\]'
            ;;
            c)
            control='\[33[1;34m\]'
            ;;
            d)
            control='\[33[1;35m\]'
            ;;
            e)
            control='\[33[1;36m\]'
            ;;
            f)
            control='\[33[1;37m\]'
            ;;
        esac
        hash=${hash:1}

        fullstr="$fullstr$control$l"
    done
    fullstr="\[33[1;31m\]\\u@$fullstr$endcontrol \[33[1;34m\]\W \$ \[33[00m\]"
    export PS1=$fullstr
}

colorps1

Advertisement

Written by revjohnnyhealey

August 24, 2009 at 11:31 pm

Posted in bash, Uncategorized

One Response

Subscribe to comments with RSS.

  1. I love this function. I had to replace your control character assumptions with tput statements (which is the correct way to modify a terminal). I added a conditional to make usernames green unless you are root, then it’s red. Finally, I trimmed some extraneous fluff.

    function colorps1()
    {
        word=$(hostname --short)
        hash=$(echo "$word" | md5sum)
        fullstr=""
        for l in $(echo $word | sed 's/\(...\)/\1\n/g'); do
            control=""
            endcontrol=$(tput sgr0)
            case "${hash:0:1}" in
                0)
                    control=$(tput bold;tput setaf 0);;
                1)
                    control=$(tput setaf 1);;
                2)
                    control=$(tput setaf 2);;
                3)
                    control=$(tput setaf 3);;
                4)
                    control=$(tput setaf 4);;
                5)
                    control=$(tput setaf 5);;
                6)
                    control=$(tput setaf 6);;
                7)
                    control=$(tput setaf 7);;
                8)
                    control=$(tput bold;tput setaf 0);;
                9)
                    control=$(tput bold;tput setaf 1);;
                a)
                    control=$(tput bold;tput setaf 2);;
                b)
                    control=$(tput bold;tput setaf 3);;
                c)
                    control=$(tput bold;tput setaf 4);;
                d)
                    control=$(tput bold;tput setaf 5);;
                e)
                    control=$(tput bold;tput setaf 6);;
                f)
                    control=$(tput bold;tput setaf 7);;
            esac
            hash=${hash:1}
            fullstr="$fullstr$control$l"
        done
        export PS1=$(tput bold;[[ $(id -un) == "root" ]] && tput setaf 1 || tput setaf 2)"\\u@$fullstr$endcontrol "$(tput bold;tput setaf 4)"\W \$ "$(tput sgr0)
    }
    

    Richard Bronosky

    November 29, 2009 at 5:37 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.