Search this site


Metadata

Articles

Projects

Presentations

Neat ksh prompt

My prompt is something in the form of:
hostname(/path/to/pwd) [###] >

Unless the previous command that was executed returned an exit code other than 0, in which case it will display the exit code just before the > as !#! where # is the exit code.

from my .profile

[ -w "/" ] && Z="#" || Z=">"
EXITCODE='$(A=$? && [ $A -ne 0 ] && echo "!!${A}!! ")'
PS1='$HOST($(/bin/pwd | sed -Ee "s,(/usr)?$HOME,~,")) [!] '$EXITCODE'$Z '

This will let you use pwd and substitute ~ for your home directory. (pdksh currently doesn't support something like this, so the hack was needed)

That's it, here's an example of what it'll look like:

nightfall(~) [471] > 
nightfall(~) [471] > echo "hi there"
hi there
nightfall(~) [472] > ls -asoijo23i5j23
ls: illegal option -- j
usage: ls [-ABCFGHLPRTWabcdfghiklnoqrstu1] [file ...]
nightfall(~) [473] !1! > thisisnotacommand
ksh: thisisnotacommand: not found
nightfall(~) [474] !127! > cd bin
nightfall(~/bin) [475] > cd /nope
ksh: cd: /nope - No such file or directory
nightfall(~/bin) [476] !1! > cd /usr/local/
nightfall(/usr/local) [477] > 

Note the !1! and !127!. These are the exit codes of the previous commands.