User Tools

Site Tools


start

This blog is my notebook for computer and technology related information, copies of useful webcontent (with refs of course), troubleshooting, general tips and setups that work well for me.

Useful tags:

Blog

The 'ultimate' bash prompt

Of course, there is not one ultimate bash prompt that fits everyone, but I'm very happy with the following prompt.

Features:

  • user/hostname color coded for differnt hosts.
  • If running in a superuser shell, the # sign is red
  • the path is truncated to the last 25 characters

How to use it:

  • copy-paste the code below into your .bashrc or, better into .bash_aliases which won't be overwritten.
  • add the hostname of your servers after the respective color
  • restart the terminal and it should work right away.
bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    NEW_PWD=${PWD/#$HOME/\~}
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
        NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
    fi
    PROMPT_ROOT='0m'
    if [ ${UID} -eq 0 ]; then
        PROMPT_ROOT='1;31m'
    fi
}
 
function setprompt()
{
    # host dependent prompt color, lookup table is below
    #default color is white
    hostnamecolor="01;37"
    while read cn cl hn
    do
        if [ "`hostname`" == "$hn" ] ; then
            HNCOL="\[\033[${cl}m\]"
    #        echo found $hostnamecolor
        fi
    done <<EOM
Black           0;30 
Blue            0;34 server1
Green           0;32
Cyan            0;36 server2
Red             0;31 
Purple          0;35 ws1
Brown           0;33 ws2
Light_Gray      0;37 
Dark_Gray       1;30 
Light_Blue      1;34 
Light_Green     1;32 
Light_Cyan      1;36 betelgeuze
Light_Red       1;31 
Light_Purple    1;35 
Yellow          1;33 
White           1;37 
EOM
 
    # ANSI color codes
    local RS="\[\033[0m\]"    # reset
    local HC="\[\033[1m\]"    # hicolor
    local UL="\[\033[4m\]"    # underline
    local INV="\[\033[7m\]"   # inverse background and foreground
    local FBLK="\[\033[30m\]" # foreground black
    local FRED="\[\033[31m\]" # foreground red
    local FGRN="\[\033[32m\]" # foreground green
    local FYEL="\[\033[33m\]" # foreground yellow
    local FBLE="\[\033[34m\]" # foreground blue
    local FMAG="\[\033[35m\]" # foreground magenta
    local FCYN="\[\033[36m\]" # foreground cyan
    local FWHT="\[\033[37m\]" # foreground white
    local BBLK="\[\033[40m\]" # background black
    local BRED="\[\033[41m\]" # background red
    local BGRN="\[\033[42m\]" # background green
    local BYEL="\[\033[43m\]" # background yellow
    local BBLE="\[\033[44m\]" # background blue
    local BMAG="\[\033[45m\]" # background magenta
    local BCYN="\[\033[46m\]" # background cyan
    local BWHT="\[\033[47m\]" # background white
 
#    PS1="$HC$HNCOL\u@\h$RS:\w]\$ "
#    PS1="$HC$HNCOL\h$RS:\w]\$ "
    PS1="$HC$HNCOL\u@\h$RS:\${NEW_PWD}]\[\e[\${PROMPT_ROOT}\]\\$ $RS"
}
 
setprompt
PROMPT_COMMAND=bash_prompt_command

~~LINKBACK~~

2014-04-24 10:40 · brb · 0 Comments

Archive

Blog History

2009-05: 18 entries 2009-06: 12 entries 2009-07: 9 entries 2009-08: 5 entries 2009-09: 2 entries 2009-10: 2 entries 2009-11: 5 entries 2009-12: 4 entries 2010-01: 1 entry 2010-02: 3 entries 2010-03: 2 entries 2010-04: 2 entries 2010-05: 3 entries 2010-06: 10 entries 2010-07: 8 entries 2010-08: 5 entries 2010-09: 4 entries 2010-10: 6 entries 2010-11: 2 entries 2010-12: 6 entries 2011-01: 5 entries 2011-02: 3 entries 2011-03: 5 entries 2011-04: 2 entries 2011-05: 4 entries 2011-06: 5 entries 2011-07: 11 entries 2011-08: 12 entries 2011-09: 4 entries 2011-10: 4 entries 2011-11: 2 entries 2011-12: 1 entry 2012-01: 5 entries 2012-02: 2 entries 2012-04: 3 entries 2012-06: 1 entry 2012-09: 4 entries 2012-10: 1 entry 2013-04: 1 entry 2014-04: 10 entries 2014-08: 2 entries 2015-04: 1 entry 2015-09: 3 entries 2016-04: 1 entry 2016-05: 1 entry 2016-06: 1 entry 2016-07: 2 entries 2016-10: 1 entry 2017-01: 1 entry 2017-02: 2 entries 2017-04: 1 entry 2017-05: 1 entry 2017-07: 2 entries 2017-08: 1 entry 2017-10: 1 entry 2017-11: 1 entry 2017-12: 2 entries 2018-10: 1 entry 2019-02: 1 entry 2019-05: 1 entry 2019-09: 1 entry 2019-10: 2 entries 2020-08: 1 entry 2020-01: 1 entry 2021-01: 1 entry 2021-02: 1 entry 2021-07: 1 entry 2022-05: 2 entries 2022-06: 1 entry 2022-11: 1 entry 2023-05: 1 entry 2023-09: 2 entries 2023-12: 2 entries 2024-09: 1 entry

2024

September

2023

December

September

May

2022

November

June

May

2021

July

February

January

2020

January

August

2019

October

September

May

February

2018

October

2017

December

November

October

August

July

May

April

February

January

2016

October

July

June

May

April

2015

September

April

2014

August

April

2013

April

2012

October

September

June

April

February

January

2011

December

November

October

September

August

July

June

May

April

March

February

January

2010

December

November

October

September

August

July

June

May

April

March

February

January

2009

December

November

October

September

August

July

June

May

start.txt · Last modified: 2022-11-29 08:28 by brb