Bashrc Prompt Config

It's always interesting seeing how people configure their systems to their liking, so I'm sharing the config for my simple bash prompt.

The prompt that I use is context-sensitive; it's shortened to not show the hostname, unless connected to by SSH, by checking the SSH_CLIENT variable.

Example prompts

Examples of prompts in different contexts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
force_color_prompt=yes

prompt_newline="${prompt_newline:-"no"}"
prompt_extranewline="${prompt_extranewline:-"no"}"

sudo_user_enable=${sudo_user_enable:-"yes"}

if [ -n "$force_color_prompt" ]; then
	if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
		color_prompt=yes
	else
		color_prompt=
	fi
fi

user_color=""
host_color=""
host=""

if [ "$color_prompt" = "yes" ]; then
	user_color="\[\e[1;32m\]"
	[ $(id -u) -eq 0 ] && user_color="\[\e[1;31m\]"

	host_color="\[\e[1;32m\]"
fi

if [ -n "$SSH_CLIENT" ]; then
	# add hostname if using ssh

	if [ "$color_prompt" = "yes" ]; then
		host_color="\[\e[1;35m\]"
		host="\[\e[0m\]"
	fi

	host+="@${host_color}\h"
elif [ -n "$SUDO_USER" ] && [ "$sudo_user_enable" = "yes" ]; then
	# add SUDO_USER if set

	if [ "$color_prompt" = "yes" ]; then
		host_color="\[\e[1;35m\]"
		host="\[\e[0m\]"
	fi

	host+="&${host_color}($SUDO_USER)"
fi

PS1=''
[ "$prompt_newline" = "yes" ] && [ "$prompt_extranewline" = "yes" ] && PS1+='\n'

if [ "$color_prompt" = yes ]; then
	PS1+='\[\e[33m\]'"${user_color}"'\u'"${host}"'\[\e[0m\]:\[\e[1;33m\]\w\[\e[1;36m\]$(__git_ps1 " (%s)")\[\e[0m\]'
else
	PS1+='\u'"${host}"':\w$(__git_ps1 " (%s)")'
fi

[ "$prompt_newline" = "yes" ] && PS1+='\n'
PS1+='\$ '

unset user_color
unset host_color
unset host

unset prompt_newline prompt_extranewline
unset sudo_user_enable

unset color_prompt force_color_prompt

# if this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
	PS1="\[\e]0;\u@\h: \w\a\]$PS1"
	;;
*)
	;;
esac