My ksh Prompt
2026-05-14

Here is my shell prompt for ksh:

PS1='\[\e[7m\]${?#0}\[\e[0m\]\W\$ '

(also works with bash & Busybox’s ash)

It looks like this: demo of my prompt

It’s nothing fancy, I know, but I’m going to explain what each part of that ugly string does anyway.

Showing the Exit Status

An exit status of 0 means your command ran without a hitch, and any other number is an error code. For some reason it is standard to hide this info from the user. Sure, you could query the exit status with echo $?, but that’d require you to have a premonition of your command failing. On top of that, the $? variable gets overwritten as soon as you run your next command.

This inclusion might be the only unique thing about my prompt.

Parameter Substitution

I use ${?#0} instead of just $? to avoid unnecessary noise. This strips the leading 0 from the variable, leaving an empty string when everything goes right while still displaying the exit status when it doesn’t.

The shell’s builtin substitutions can be used to avoid calls to external programs and subshells in some scripts. Here’s a simple example of parameter substitution compared to plain old sed being used to strip the protocol from a URL:

~$ URL=http://example.net
~$ echo $URL | sed 's!^http://!!'
example.net
~$ echo ${URL#http://}
example.net

ANSI Escape Sequences

Before getting into the actual ANSI stuff, \[ and \] are there to inform the shell that the enclosed characters take up no physical space on the screen. Without these, the shell will get confused about line wrapping and cursor placement.

ANSI Escape Codes, on the other hand, are interpreted by the terminal emulator as instructions for styling text. I use \e[7m to tell the terminal to invert the foreground and background colors when outputting the exit status to make it pop out a bit. After that, I use \e[0m to reset the terminal to its default state.

Here’s a snippet that spews out all of the colors you can jam into your prompt:

i=0
while [ "$i" -le 255 ]; do
    printf "\e[48;5;%sm %3d \e[0m" "$i" "$i"
    i=$((i + 1))
done
printf "\n"

Despite the possibilities, I generally skip color in my terminal. The same goes for emojis, ligatures, and Nerd Fonts. :)

The Rest

\W provides the current directory name. I prefer this over having the full path (\w). I saw a script that truncates directory names to keep your prompt short (e.g., ~/.c/mpv). While it looks like a nice middle ground, including a subshell in my prompt feels frivolous.

\$ displays $ for regular users and # for root. In some odd way, the root symbol could act as a safety against copy-pasting commands since what follows is read as a comment. Is this a mere coincidence, or divine providence offering a reckless soul the opportunity to repent before ignorantly executing a command from a forum as root?

Finally, I add a space at the end too. It gives a bit of breathing room between the prompt and the command.

Additional Resources