Take command of your BASH prompt
posted Sep 5th 2009 2:37pm by Mike Szczysfiled under: linux hacks

[Joshua] has put together a list of BASH prompt customizations. The command prompt is used in a command-line interface to show that the system is ready for the next command. Often times this is nothing more than a user name, host name, and working directory:
mike@krusty:~$
[Joshua's] customization examples can be used to color code the information in your prompt, change what information is displayed, and make the prompt respond differently when an invalid command is typed. A BASH prompt reference is helpful in deciphering what each of these commands do. The easiest simplification is to understand that non-printing characters (such as color codes) are surrounded in escaped square brackets. For example, line 1 is the sequence for Red, line 2 is the sequence for Dark Grey, and line 3 sets a simple prompt to display in Red and all text after that to be in Dark Grey:
\[\e[0;31m\] \[\e[1;33m\] PS1="\[\e[0;31m\]\u@\h:\w\$ \[\e[1;30m\]"

Above you will notice an added notification that we typed an invalid command. This prompt is produced with the following code:
PS1="\`if [ \$? != 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]===---; fi\`\n\[\e[1;30m\]XX \[\e[0;32m\]Hack a Day \[\e[1;30m\]XX\n\[\e[0;37m\][\[\e[1;31m\]\@\[\e[0;37m\]] \[\e[0;32m\]\u@\h \[\e[0;37m\][\[\e[1;34m\]\w\[\e[0;37m\]] \[\e[0;32m\]\$ \[\e[0m\] "
We use the shell a lot and this transforms the prompt from something we mostly ignore into a useful tool. Typing a command in the shell will change the prompt for the current session only. If you want a more permanent change, add the line to the bottom of your ~/.bashrc file.
[via Digg]








BASH, so… avergage. If you wanted better promtage, you’d most likely be using zsh. Its really a shame more people dont know of the wonders that zsh has, so many people dragooned into mediocrity.