As part of my last post, I wrote this mini-guide about my PS1, but I figured I’d separate it into a separate page.
\n\[\e[33;1m\]#(\[\e[31;1m\]`if [ $? = 0 ]; then echo ‘\[\e[32;1m\]^_^\[\e[0m\]‘; else echo ‘\[\e[31;1m\]O_O\[\e[0m\]‘; fi`\[\e[33;1m\])-(\[\e[34;1m\]!\!\[\e[33;1m\])-(\[\e[34;1m\]\u@\H\[\e[33;1m\])(\[\e[34;1m\]\T\[\e[33;1m\])-(\[\e[34;1m\]\[\e[33;1m\])\n#\[\e[1;32m\](\w)\[\e[33;1m\]\n\[\e[0;32m\]
Now I know it looks a bit daunting, but when I wrote it I had the ASCII colors mapped to variables (done in my .bashrc) so I could do it.
Here is my .bashrc (a bit weird because we use C shell by default, so my .login just spawns a bash shell):
txtblk=’\e[30;1m' # Black - Regulan
txtred='\e[31;1m' # Red
txtgrn='\e[32;1m' # Green
txtylw='\e[33;1m' # Yellow
txtblu='\e[34;1m' # Blue
txtpur='\e[35;1m' # Purple
txtcyn='\e[36;1m' # Cyan
txtwht='\e[33;1m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
txtgrn2='\e[0;32m' # Other Green
IP=`curl -s www.whatismyip.org`
PS1="\n\[$txtwht\]#(\[$txtred\]\`if [ \$? = 0 ]; then echo ‘\[$txtgrn\]^_^\[\e[0m\]‘; else echo ‘\[$txtred\]O_O\[\e[0m\]‘; fi\`\[$txtwht\])-(\[$txtblu\]!\!\[$txtwht\])-(\[$txtblu\]\u@\H\[$txtwht\])(\[$txtblu\]\T\[$txtwht\])-(\[$txtblu\]$IP\[$txtylw\])\n#\[$bldgrn\](\w)\[$txtwht\]\n\[$txtgrn2\]”
Now my PS1 is a bit more readable (only if you really know what you’re looking at though).
This is what it looks like:
Before we pull the PS1 apart, lets be clear about what is going on. Firstly is the #’s, they seem just for show, but when I’m scripting it’s a lot easier for me to copy entire blocks, and just use sed to remove all the lines that start with a #, work smart. Not hard.
The next section is a graphical representation of the last program’s exit status. I didn’t really realize how much of an impact this would have on my scripting but now I use exit codes a LOT more, which allows for greater flexibility.
^_^ = True (exit 0) – 0_0 = False (exit !0)
Next we have the command number I’m on. This is handy because if I ever want to repeat a command, instead of selecting the entire thing, I can just type in !XXX where XXX is the command number, and it will look it up in the history and execute!
After that we have (user@hostname) & (time) – self explanatory
Then with a bit of cURL magic we grab our external IP address:
IP=`curl -s www.whatismyip.org` – This is what i’ve always used but they seem down, this next line works for now
IP=`curl -s whatismyip.com/automation/n09230945NL.asp`
On the next line we just have the working directory, which makes life a LOT easier when trying to figure out where the hell you are! Especially if you have as many terminals open as I do on a daily basis!
Alright, so now that we know what our PS1 is doing, we can try to understand it.
The first relevant part is this:
`if [ \$? = 0 ]; then echo ‘\[$txtgrn\]^_^\[\e[0m\]‘; else echo ‘\[$txtred\]O_O\[\e[0m\]‘; fi\`
This is a logic test to check the $? variable (the exit status of the last program) and selects either the ^_^ or the 0_0
The rest of it is pretty much just built-in’s
\[$txtwht\])-(\[$txtblu\]!\!\[$txtwht\])-(\[$txtblu\]\u@\H\[$txtwht\])(\[$txtblu\]\T\[$txtwht\])-(\[$txtblu\]$IP\[$txtylw\])\n#\[$bldgrn\](\w)\[$txtwht\]\n\[$txtgrn2\]”
Here’s a list of built-ins:
- \a : an ASCII bell character (07)
- \d : the date in “Weekday Month Date” format (e.g., “Tue May 26″)
- \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
- \e : an ASCII escape character (033)
- \h : the hostname up to the first ‘.’
- \H : the hostname
- \j : the number of jobs currently managed by the shell
- \l : the basename of the shell’s terminal device name
- \n : newline
- \r : carriage return
- \s : the name of the shell, the basename of $0 (the portion following the final slash)
- \t : the current time in 24-hour HH:MM:SS format
- \T : the current time in 12-hour HH:MM:SS format
- \@ : the current time in 12-hour am/pm format
- \A : the current time in 24-hour HH:MM format
- \u : the username of the current user
- \v : the version of bash (e.g., 2.00)
- \V : the release of bash, version + patch level (e.g., 2.00.0)
- \w : the current working directory, with $HOME abbreviated with a tilde
- \W : the basename of the current working directory, with $HOME abbreviated with a tilde
- \! : the history number of this command
- \# : the command number of this command
- \$ : if the effective UID is 0, a #, otherwise a $
- \nnn : the character corresponding to the octal number nnn
- \\ : a backslash
- \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
- \] : end a sequence of non-printing characters



