November 15, 2004

CLI Magic: prompt mods

Author: Joe Barr

One of the OSTG editors pointed out to me recently that you can make a lot of modifications to the console to make it more attractive and readable without "going over the top" like some felt I did with the column on aterm. He had a good point. Pull the reins in on that runaway ninny that Linux GUI environments have become, and join me on the CLI to take a look at what he meant.

Stock SUSE bash prompt

The stock bash prompt on my SUSE 9.2 Professional looks like this:


That prompt is dynamically created -- the user name, machine name, path, and prompt character are inserted by the script located at /etc/bash.bashrc. Here is part of the code snippet that defines the prompt:

 if test "$UID" = 0 ; then
            _p=" #"

The if statement above (I've removed part of the code which is not essential to the story) checks to see if it is the root account (UID = 0). If it is, the user name is dropped from the prompt by setting _u to "\h" and the prompt character used to be used for root is set to "#". The \h will be interpreted by the bash shell as meaning print the host name.

If it's not root, then the prompt will include (like mine does above) both the user name (warthawg) and the host name (linux), and the prompt character is set to ">". The prompt is also defined to include an abbreviated form of the current directory when PS1 to set as follows:

PS1="${_t}${_u}:\w${_p} "

Customizing the prompt

You can instantly change the prompt to something else by exporting a new PS1 variable. For instance, entering the following command at the CLI:

warthawg@linux:~> export PS1='ElPrompt***>>>'

Immediately changes the prompt to look like this:


The only problem with setting the prompt as shown above is that it doesn't stick. As soon as you log out, it's gone, and the next time you log in it will be the same as when you started. But there is a way to make your changes stick.

In SUSE, the place to do it is in the .bashrc, or the .profile file, in your home directory. Personally, I like to keep all my personalized things in one basket, so I use .bashrc and leave .profile alone. But use one or the other of the files in your home directory. They have a much better chance of surviving your next system upgrade than does /etc/bash.bashrc or /etc/profile. Not to mention, changes made to the /etc are system wide instead of just for you.

Remember the \u and the \h set in the stock script? We can change the prompt back to something similar to its starting point by entering the following command at the CLI:

ElPrompt***>>>export PS1='\u@\h\$'

Which results in the following:


Unless you're root, that is. In that case the prompt would display a # instead of a $.

You can go crazy with some of the other options. Here's a few that are fun to play with:

  • \d displays the current date
  • \t displays the current time
  • \l the shell's terminal device

Let's try those three and see what happens. Enter this:

export PS1='\d \t \l \u\$'

The resulting prompt looks like this:

Tue Nov 09 14:52:21 1 warthawg$

Thanks, Daniel Robbins and IBM

My starting point for this week's column was Daniel Robbins' article entitled "Prompt Magic" at IBM's DeveloperWorks Linux site.

Click Here!