This post explains what I’m referring to by color coded bash prompt, why it is useful to have one, and presents an implementation.
By color coded bash prompt, I am referring to the use of colors to represent aspects of the environment running bash. An ordinary bash prompt may display a username and hostname. Color coding can be used to represent whether the user is root, whether the session is running on a local or remote machine, and other aspects of the environment (not covered in this post).
When running multiple terminals at the same time, with some connected to remote machines and/or running as root, color coding makes it easier to keep track of the sessions, and may help prevent inadvertently entering a command as root or on the wrong machine.
I have configured my bash prompt’s colors to be a function of whether I am running as a root or non-root user, and whether I am connected to a local or remote machine. The username is displayed in green for a non-root user and in red for the root user. The hostname is displayed in blue for a local bash session and in cyan for a remote session.
The following image shows the four possible scenarios, 1) on a local machine as a non-root user, 2) on a local machine as root, 3) on a remote machine as a non-root user, and 4) on a remote machine as root.
Color coding can be added by putting the following snippet of code in your non-root user’s .bashrc, both locally and on remote machines. In the session shown in the image above, sudo -s was used to become root. Using sudo su would not have worked unless the code was also added to root’s .bashrc or to a global bashrc. By switching to root with sudo -s or sudo bash instead of sudo su, root’s shell will contain the environment provided by the user’s .bashrc.
Some of the functions for determining whether the current shell is running in an ssh session are from a response on Unix Stack Exchange. I could not figure out a way to reliably determine whether the running shell is in an ssh session when the shell is running within xterm, so in that case the hostname is displayed in the default color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fails to properly return true when running sudo -s | |
function is_ssh_from_env() { | |
if [ -n "${SSH_TTY}" ] || \ | |
[ -n "${SSH_CONNECTION}" ] || \ | |
[ -n "${SSH_CLIENT}" ]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
# fails to properly return true using xterm | |
function is_ssh_from_who() { | |
# method doesn't work in an xterm so just return false | |
if [ -n "${XTERM_SHELL}" ]; then | |
echo "false" | |
return | |
fi | |
if [[ $(who -m) =~ \([-:a-zA-Z0-9\.]+\)$ ]]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
# fails to return true when not running from linux or when connected to a | |
# screen not started under ssh | |
function is_ssh_from_ppid() { | |
# only works with /proc directory. otherwise return false | |
p=${1:-$PPID} | |
if [ -f /proc/$p/stat ]; then | |
read pid name x ppid y < <( cat /proc/$p/stat ) | |
[[ "$name" =~ sshd ]] && { echo "true"; return; } | |
[ "$ppid" -le 1 ] && { echo "false"; return; } | |
is_ssh_from_ppid $ppid | |
else | |
echo "false" | |
fi | |
} | |
# not totally reliable but works in a lot of cases (may not work in xterms) | |
# also the functions it relies on don't return any known false positives, | |
# only false negatives | |
function is_ssh() { | |
if "$(is_ssh_from_env)" || \ | |
"$(is_ssh_from_who)" || \ | |
"$(is_ssh_from_ppid)"; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
function is_xterm() { | |
if [ -n "${XTERM_SHELL}" ] || \ | |
[ -n "${XTERM_LOCALE}" ] || \ | |
[ -n "${XTERM_VERSION}" ]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
# root prompt red (even in ssh session since if you sudo -s in an ssh session | |
# there seemingly isn't a way to know you're in an ssh session) | |
if [ "root" == "$(whoami)" ]; then | |
USER_COLOR="31m" | |
else | |
USER_COLOR="32m" | |
fi | |
# if you're running in an xterm, you can't reliably determine remote status, | |
# so just use default color | |
if "$(is_xterm)"; then | |
HOST_COLOR="0m" | |
elif "$(is_ssh)"; then | |
HOST_COLOR="36m" | |
else | |
HOST_COLOR="34m" | |
fi | |
PS1="\n\[\e[${USER_COLOR}\]\u\[\e[0m\]@\[\e[${HOST_COLOR}\]\h " | |
PS1+="\[\e[33m\]\w\[\e[0m\]\n\$ " |