For years, I was totally happy to have a minimal, uninformative shell (bash) prompt. It wasn't that I didn't like information at my finger tips every time I'd press enter. I just found that most of the information got in the way of what I was trying to do. Then a few years ago, a co-worker put together (at least I assume he did, he might have found it elsewhere) a very informative and attractive shell prompt that disclosed the boxes vitals every time a command returned. Now, it's almost a necessity for me.
What makes a good shell prompt a good shell prompt? Well, first and foremost, it should disclose what box you actually are on and what is your present working directory is. Having these two things is probably the single most important way to avoid disaster since 90% of the time, you're logged into multiple boxes doing multiple things and knowing where you are typing on and in what context is essential! Additional data points are fluff, but having load averages can sure be handy when you're wondering why a box is nearly unresponsive.
Beauty is, of course, in the eye of the beholder. In my experience, ANSI colors make the terminal a better place to be and make it easier to identify different data types or points. Using colors in this way in your shell prompt help aesthetics and your ability to identify what information you're looking at. My favorite prompt uses colors liberally and sometimes takes people off guard when they rubberneck on my screen but I assure you it has as much function as form.
Obviously, the best prompt for you is decided by you alone but I'll share what my best prompt is. I've used this prompt on Linux for years and with my recent FreeBSD setup, I tweaked it to work on that as well. Here's what my prompt looks like...
For non-root:

For root:

What we have above is a good amount of information in each prompt that is of the second that the prompt is generated (upon completion of your last command). From left to right: user@host, load averages 1m/5m/15m, memory usage, uptime, ISO date, followed by pwd on the next line. The use of colors makes each value clearly identifiable and also easy to tell whether you are root or not. I've found this prompt highly useful in a number of scenarios, like say when you're looking back at your scrollback for exact times of incidents and recovery measures.
I put all the setup for this prompt in my /etc/bash/bashrc for Linux. I'll share this file in its entirety with you at the end of this post but really the prompt setup comprises of a few parts...
1) Define your colors. You need a palette to work with and this gives you the ANSI control codes to pull them off in your shell...
BLACK="\[\033[0;30m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
BROWN="\[\033[0;33m\]"
BLUE="\[\033[0;34m\]"
PURPLE="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
LIGHT_GRAY="\[\033[0;37m\]"
GRAY="\[\033[1;30m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
YELLOW="\[\033[1;33m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
LIGHT_CYAN="\[\033[1;36m\]"
WHITE="\[\033[1;37m\]"
UNDERLINE="\[\033[4m\]"
NO_COLOR="\[\033[0m\]"
2) Now that you have variables defined for the colors, you can then define your preference for colors for parts of the prompt and also the difference between a root and non-root prompt:
C_L1=$RED
C_L5=$GREEN
C_L15=$BROWN
C_PROC=$CYAN
C_UPTIME=$PURPLE
if [[ ${EUID} == 0 ]] ; then
C_PRI=$LIGHT_RED
C_PARENS=$BROWN
else
C_PRI=$LIGHT_GREEN
C_PARENS=$LIGHT_BLUE
fi
3) Finally, set the meat of it. Our PS1, the prompt itself:
PSHOST=`uname -n`
PS1="\n$C_PRI-$C_PARENS[$C_PRI\u$C_PARENS@$C_PRI$PSHOST$C_PARENS]$C_PRI-$C_PARENS[$C_L1\$(cut -d' ' -f1 /proc/loadavg)$C_PARENS/$C_L5\$(cut -d' ' -f2 /proc/loadavg)$C_PARENS/$C_L15\$(cut -d' ' -f3 /proc/loadavg)$C_PARENS]$C_PRI-$C_PARENS$C_PROC\$(free | xargs echo | awk '{print(int(((\$9+\$20-\$13)*100)/(\$19+\$8))\"%\")}')$C_PARENS$C_PRI-$C_PARENS$C_UPTIME\$(U=\$(cut -d'.' -f1 /proc/uptime);echo \$((\$U / 86400))d\$((\$U / 3600 % 24))h\$((\$U / 60 % 60))m)$C_PARENS$C_PRI-$C_PARENS$C_PRI\D{%Y-%m-%d}${C_PARENS}T${C_PRI}\\t$C_PARENS$C_PRI-\n$C_PRI-$C_PARENS[$C_PRI\w$C_PARENS:$C_PRI\\$""$C_PARENS]$C_PRI- $NO_COLOR"
4) Though this will not directly work on FreeBSD, you'll have to have linprocfs mounted and use this slightly tweaked version:
PS1="\n$C_PRI-$C_PARENS[$C_PRI\u$C_PARENS@$C_PRI$PSHOST$C_PARENS]$C_PRI-$C_PARENS[$C_L1\$(cut -d' ' -f1 /compat/linux/proc/loadavg)$C_PARENS/$C_L5\$(cut -d' ' -f2 /compat/linux/proc/loadavg)$C_PARENS/$C_L15\$(cut -d' ' -f3 /compat/linux/proc/loadavg)$C_PARENS]$C_PRI-$C_PARENS$C_PROC\$(cat /compat/linux/proc/meminfo | xargs echo | awk '{print(int(((\$9+\$20-\$13)*100)/(\$19+\$8))\"%\")}')$C_PARENS$C_PRI-$C_PARENS$C_UPTIME\$(U=\$(cut -d'.' -f1 /compat/linux/proc/uptime);echo \$((\$U / 86400))d\$((\$U / 3600 % 24))h\$((\$U / 60 % 60))m)$C_PARENS$C_PRI-$C_PARENS$C_PRI\D{%Y-%m-%d}${C_PARENS}T${C_PRI}\\t$C_PARENS$C_PRI-\n$C_PRI-$C_PARENS[$C_PRI\w$C_PARENS:$C_PRI\\$""$C_PARENS]$C_PRI- $NO_COLOR"
And that's it. I hope this has inspired you to use a better, more versatile prompt than what you typically get with your Linux/Unix distribution which typically just gives you a host and a pwd.
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
No feedback yet
Leave a comment
