# A custom(izable) bash prompt



## PEZIUS (Dec 4, 2002)

Hi,
I posted this on the macosxhints Unix forum, but noboy seems to give a damn for it. Maybe someone here can give me some feedback. 

I've jazzed up the bash prompt . Where I work we have mirrored directory structures for development and production. We also have something called "installation id" which, well identifies the installation. It's good to see where you stand without having the full path in the prompt since the directory structures are rather deep. Which track (development or production) is decided on "level" 2 in the path and which installation-id is on level 4. A path "depth" of 2 is my preference. I use this prompt like so:

$ pwd
/home/dev/data/inst1/surveys_edit/213/logs
$ P_DEPTH=2
$ P_HL_LEVELS="2/4"
$ . ~/.my_bash_prompt

Which gives me a prompt like this:

*pez@hostname dev inst1 213/logs $*

If I want to change the depth to, say 3, I do:

*pez@hostname dev inst1 213/logs $ pd 3*
*pez@hostname dev inst1 surveys_edit/213/logs $*

If I don't want the highlighted thingy:

*pez@hostname dev inst1 surveys_edit/213/logs $ phl*

*pez@hostname surveys_edit/213/logs $*

"pd" and "phl" are two utility functions defined in the same file as the prompt.

The file "bash_prompt" is sourced from /etc/bashrc on the development servers at my job and looks like this:


```
# PEZ's bash prompt
# Features a user defined path "depth" and can highlight portions
# of the path at the users choice. The user functions pd and phl 
# are shortcuts to setting the variables P_DEPTH and P_HL_LEVELS
# respectively. Set these variables before sourcing this file.

[ -z "$P_DEPTH" ] && P_DEPTH=2
[ -z "$P_HL_LEVELS" ] && P_HL_LEVELS="2/4"

# pd [depth]
# Set prompt depth
# Without arguments you get a full path prompt (i.e. "\w")
function pd() {
    P_DEPTH="$1"
}

# phl [levels]
# Set prompt highlight levels as a "/" delimited string.
# Without arguments it switches off the highlight thingy.
function phl() {
    P_HL_LEVELS="$1"
}

PS1='$(
    P_HL_DECORATION="\[\033[1;34m\]"
    P_NORMAL_DECORATION="\[\033[0m\]"
    IFS="/"
    P="\w"
    HL=""
    p=($(echo "\w"))
    e=$((${#p[@]}-1))
    if [ -n "$P_HL_LEVELS" ]; then
	hl_levels=($P_HL_LEVELS)
	HL="$P_HL_DECORATION"
	for ((l=0; ${#hl_levels[@]} - $l; l++)); do
	    HL="$HL${p[${hl_levels[$l]}]} "
	done
	HL="$HL$P_NORMAL_DECORATION"
    fi
    if [ $P_DEPTH -lt $e ]; then
	b=$(($e-$P_DEPTH+1))
	P="${p[$((b++))]}"
	for ((;$e-$b+1; b++)); do
	    P="$P/${p[$b]}"
	done
    fi
    echo -n "\u@\h ${HL}$P \$ "
)'
```

/PEZ


----------

