Back to main page

The Quick-N-Dirty Guide to Shell Prompts

The aim of these QND guides is to get you up and running quickly. There will be far more howto than why here.

This page covers making slightly more informative prompts with the bash, zsh and ksh shells. References are given at the end of the page, for those who wish to explore it more thoroughly.

For purposes of example, I am going to use the Gentoo Linux default prompt. It works well for me, showing the user, the machine name up to the first period and the current working directory. These three pieces of information are in different colors.

Bash

As bash is probably more popular than the other two shells, let us begin with it. A quick note--the current working directory in bash used to be designated by a \W. However, as of bash3, using a \W gives you a ~ for the home directory. As I age, and my eyes get worse, I like to know where I am. The prompt given below will work with both bash-2.x and 3.x

So, let us say that I am logged in as user srobbins on the machine nyserve1.homeunix.net. I cd into my html directory. I want the prompt to show

srobbins@nyserve1 html $ 

I also want the srobbins@ to be one color the nyserve1 to be a different color and the working directory and prompt to be a third color so that it looks like this

srobbins@nyserve1 html $

In bash, I would do it this way.

Edit your .bashrc in your home directory. Make sure your .bash_profile contains the line
source ~/.bashrc

(If these files don't exist, create them.)

PS1="\[\033[1;32m\]\u@\[\033[1;31m\]\h \[\033[1;36m\]\${PWD##*/} $\[\033[0m\] "

If your browser broke that, it should be on one line. A quick explanation here. PS1 is your main prompt. We're doublequoting here (") to preserve variable expansion. If you did a single quote (') your prompt will have ${PWD} instead of giving you your working directory.

The \[\033 begins a color sequence. The [1;32 specifies a color and the m\] ends the sequence. The 1 of the 1;32 makes this a bright green. If you had used 0;32 or just 32 it is a duller green. The \u is bash's way of denoting the user, \h is the host up to the first period. (In this case, the nyserve1 part of nyserve1.homeunix.net.

The ${PWD##*/} is a variable for printing the working directory--the ##*/ part cuts off all but the actual directory. If I left off the ##*/ part it would give my complete working directory, /home/srobbins/html in this case. The $ is the standard shell prompt, then another color sequence, ending with the 0m\]. If you leave off that 0m part, then everything after your prompt will be the same color as your prompt, bright cyan in this case. In other words, if you type a command at the prompt, the command will also appear in bright cyan.

The colors in the particular prompt have the user name and @ in bright green, the machine name in red and the working directory and prompt in cyan.

Note the space before the final quote. If I leave that out, the command will appear right after the prompt, such as

$ls

instead of

$ ls

This is a matter of personal taste. I find the single space after the prompt easier to read.

As mentioned above, I like the Gentoo default user prompt information. You can use other designators to show the time, or many other things. For a full listing, do

man bash

Once in the man page, look for PROMPTING. (As my readers might be very new to *nix, to search for a term in a man page, one types a / followed by the term--it is case sensitive so doing something like /prompting won't get you anywhere, do /PROMPTING instead.)

A list of the possible colors can be found the in the bash prompt howto, referenced at the end of this page. The colors are standard ANSI so will work in all the shells.

If you just want the information, without the colors, then leave out all the \[\033]1;32m\] stuff. In other words, this prompt, with no colors would be

PS1="\u@\h \${PWD##*/} $ "

The zsh shell

Most of the time, I use zsh. I started using it out of boredom with bash at some point and since then, I've gotten used to it. It has a few bugs, for instance writing STDERR doesn't always work properly, and a simple perl script will sometimes give odd results. However, I feel its advantages are worth these quirks. (For example, it can, unlike bash, handle floating point decimals). My zsh prompt is slightly different--rather than a $ I use %, which seems to be standard. To get the same prompt as was just shown in bash (but with the % prompt and slightly different colors) my zsh would have the line
PS1=$'%{\e[1;34m%}%n@%{\e[1;32m%}%m %{\e[1;36m%}%1d %{\e[1;36m%}%#%{\e[0m%} '

In this case, I single quote it. After the bash explanation, this should be relatively simple to understand. Some of the syntax is different, obviously. % is often used instead of \ to escape or to preface some shell names. n is used instead of u and m is used instead of h. The %1d prints the directory that you are using. Again, using our example of /home/srobbins/html had I put %2d it would show srobbins/html, etc. In some places one uses a { instead of a [. The %#% is how I get my % prompt in zsh.

In this case, the username and @ are in bright blue, the machine name in bright green and again, working directory and prompt are in bright cyan.

For further options one can do man zshmisc and search for PROMPT EXPANSION.

The ksh shell

I haven't used the korn shell in awhile, but the principles of the prompt aren't that different. However, ksh won't read a .kshrc unless you tell it to do so in your $HOME/.profile As this page is just about prompts, see my page on stupid ksh tricks referenced below, for more information on that.

With OpenBSD, the one thing that I use with ksh, however, it now uses xenodm to start X, by passing the korn shell start up files of $HOME/.profile and $HOME/.ksh. However, you can put the following in $HOME/.xsession, which is what is used now . See my page on OpenBSD and X.

To get the same information that we have been using, I have this in my $HOME/.xsession file
PS1="\[\033[1;32m\]\u@\[\033[1;31m\]\h \[\033[1;36m\]\${PWD##*/} $\[\033[0m\] " ;export PS1
Once again, we are using a $ prompt.

For further reading on all of these, you can view their respective man pages. As mentioned, in the bash page, it's under PROMPTING and for zsh it's in the zshmisc section under PROMPT EXPANSION.

For some other shellscripting links you can look at a page of mine.

For the bash prompt, there is also the bash prompt howto at the Linux documentation project which goes into great detail.

For the Korn shell, I have a page of Stupid ksh tricks

Back to main page