Saturday, September 17, 2016

colorful, useful prompts in ksh

The Prompt

The cli is the main method of access to an OpenBSD system, and by default it uses ksh as the shell provided to the user. But by itself the shell is very minimal.

Aside from being a bit user-unfriendly, it also adds no value aside from apprising the user of whether they are acting as root or not (with the change of hash from '$' to '#'). What a lonely little prompt!

Adding Value

It does no good to simply add some flair and hope that makes life better- there needs to be some actual value added or won't make sense. Things that might make sense in a prompt:
  • time/date stamp
  • location of prompt (system and path)
  • who you are (with a little emphasis when acting as root)
Whether it's an audit or troubleshooting why something won't work, having situational awareness is a valuable resource, and it shouldn't be something that's called up. It should just be there. Enter the enhanced shell prompt (that's way more dramatic than I wanted it to be. It's just a shell prompt.)

First we'll make the changes, then explain what they're about.

Working with .profile

Jump into your favorite editor and start hacking away at your user's .profile file. It's a hidden file at /home/username/.profile directory, and even root has one at /root/.profile . Once you're in it, let's change some things around. You'll see the default .profile is quite bare:

No wonder there's no love at the prompt. OK, let's start laying the groundwork- add the following color and formatting definitions to be used in the prompt definition:

e=`printf "\033"`

unset e

OK, and now for the sizzle. It's the prompt definition we'll label 'PS1':

PS1='${darkcyan}[${end}$(id -un)@\h${blue}: ${end}$(date "+%a, %h-%d %l:%M%p")${darkcyan}]${end}\n${darkcyan}${PWD}${end}${darkcyan}/${end} ${blue}#${end}'

Be sure this one line is just that - one line. Then, you'll want to use this prompt definition:

export PS1

The final result should now look more like:

(notice the difference in this screenshot versus the previous one? The exact same file is being edited in both screenshots, but in the first one the full path was specified to nano, whereas in the second a little sh shorthand is used to specify the same file in the same directory I was already in.)

You won't see these changes in action until you restart your cli session- the easy way is to log out and log back in. Then you can see what this has done to your prompt:

 Contained in each 'prompting' if you will, is the user and the system they are logged into, the time/date, and on the following line the path of the location they are in. The nice thing about the color is twofold-

  • it designates on a crowded screen where user input was entered versus return system output, making it easier to discern past user behaviors.
  • it designates the user versus another user that might have a different color scheme... namely root.
And so to riff off that last point, here's what my common root .profile shell prompt appears in my systems, a very keen contrast to a typical user:

Under the hood

So what's going on in that one line that takes advantage of the definitions before it? That's in part2!