BASH prompt galore

Here’s the latest result of trying to make a BASH prompt which might be useful if you use chroot, ssh, su or git:

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]
then
    debian_chroot=$(cat /etc/debian_chroot)
fi

chroot='${debian_chroot:+($debian_chroot)}'
PS1="$chroot"

# Color support detection from Ubuntu
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null
then
    reset='\[\e[0m\]'
    red='\[\e[1;31m\]'
    green='\[\e[1;32m\]'
    orange='\[\e[1;33m\]'
    blue='\[\e[1;34m\]'
fi

# Red user if root, orange if su
if [ "$USER" = "root" ]
then
    PS1="$PS1$red"
elif [ -n "$SUDO_USER" ]
then
    PS1="$PS1$orange"
else
    PS1="$PS1$green"
fi
PS1="${PS1}\u${reset}@"

# Red host if SSH
if [ -n "$SSH_CONNECTION" ]
then
    PS1="$PS1$red"
else
    PS1="$PS1$green"
fi
PS1="${PS1}\h${reset}:${blue}\w${reset}"

# Git branch
if [ "$(type -t __git_ps1)" = "function" ]
then
    PS1="${PS1}\$(__git_ps1 ' (%s)')"
fi

# PS1 end
if [ "$USER" = "root" ]
then
    separator='#'
else
    separator='$'
fi
PS1="${PS1}${separator} "

Example session (if the above is in ~/.bashrc or /etc/bash.bashrc on both hosts):

my_user@local_hostname:~$ cd git-repos/project/
my_user@local_hostname:~git-repos/project (master)$ sudo -u other_user bash
other_user@local_hostname:~git-repos/project (master)$ ssh third_user@other_hostname
third_user@other_hostname:~$ exit
other_user@local_hostname:~/Desktop/images$ exit
my_user@local_hostname:~/Desktop/images$
Advertisements

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 )

Google+ photo

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

Connecting to %s