Robert D. French

About Me
E-Mail
GitHub
LinkedIn
Signal
Atom Feed
blog(8) System Manager's Manual blog(8)

The default prompt in OpenBSD ksh(1) is a little underwhelming. Fortunately, you can customize it without much hassle. ksh doesn't offer the limitless config of zsh, but you can still do some pretty neat stuff.

For example, I like to have a prompt that shows my hostname and my current working directory. If you look at the ksh(1) manual entry, scroll down to the sub-subsection called "PS1". This is the environment variable you need to set in order to change the way your prompt looks. You'll see the full list of escape sequences, including the ones we need for this task: \h and \w.

Now you can run the following to change your prompt to include the hostname and the current directory:

puffy0$ export PS1="\h \w \$ "
puffy0 ~/blog $

Sometimes it's helpful to see the full hostname. For example, out-of-band management hosts that exist in different networks might all be called "bastion", so using the fully qualified domain name can help you know bastion:

puffy0 ~/blog $ export PS1="\H \w \$ "
puffy0.example.org ~/blog $
If you're vain, like me, you'll want to see your own name on the command prompt every time. You can accomplish that with the \u escape:
puffy0.example.org ~/blog $ export PS1="\H \w \$ "
robert@puffy0.example.org ~/blog $
This is fine until you start to add other decorations, or you get into a deeply-nested directory. Consider how little horizontal space remains here:
robert@puffy0.example.org ~/blog $ export PS1="[\H \w] \$ "
[robert@puffy0.example.org ~/blog] $ cd deeply/nested/directory/path
[robert@puffy0.example.org ~/blog/deeply/nested/directory/path] $

What I like to do is split my PS1 into two lines: a "situational awareness" line, containing the data above, and then a "prompt" line containing only the prompt symbol. This gives me maximum horizontal space for running convoluted shell commands. You can accomplish (no surprise!) this with the \n escape:

[robert@puffy0.example.org ~/blog] $ cd
[robert@puffy0.example.org ~] $ export PS1="[\H \w]\n\$ "
[robert@puffy0.example.org ~]
$ cd ~/blog/deeply/nested/directory/path
[robert@puffy0.example.org ~/blog/deeply/nested/directory/path]
$ echo "So much roooooooom!"
So much roooooooom!
[robert@puffy0.example.org ~/blog/deeply/nested/directory/path]
$
The upshot of this is that, no matter how deep I descend into my filesystem, I don't lose any screen real estate for my prompt. See ksh(1) for more tricks, it's a good read!
2025-04-02 Robert D. French