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.

0 responses to 'Neat ksh prompt'

Showing last 0 comments... (Click here to view all comments)


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: