How to make your Unix prompts more useful and interesting

Sandra Henry-Stocker

Had enough of $ and #? You can make your bash prompts far more interesting and likely a lot more useful by customizing them. You can even change the font color or set up your prompt to change depending on where you're sitting in the file system, what system you're working on, or what time it is.

A customized prompt can serve as a gentle reminder of what system you're working on -- especially helpful if you often find yourself logged into 3-4 systems at a time, window hopping to get several things done at the same time. or if you log in with different accounts. A customized prompt can help if you like to be reminded where you are sitting in the file system or prefer *not* to be reminded where you are in the file system (a full path can take up quite a bit of screen space when you're seven levels deep into /home/username/apps/docs/...).

All you need to do to define your PS1 variable and save it in your .bashrc or .bash_profile file. As it turns out, however, you have a lot of flexibility on what you put into that prompt.

The primary prompt PS1 can be set interactively, but is generally set in one of your dot files. The default is generally just a $, but some admins will customize this to include such things as the system name or your current location in the file system. Don't like that? Override it with seeintgs or your own. The default is usually just a > sign.

You can also set the PS2 command to change the way you're prompted when you're in a loop. You can display this value using the echo \ command.

$ echo \

Your prompt should be set up to provide the most interesting and or valuable indicator.

Can't remember what day it is? Set your prompt to the date:

$ PS1='\d> '
Sat Feb 28>

Want to be sure that you don't want to forget what time it is and end up working until midnight? Set your prompt to the current time.

$ PS1="\@> "
08:47 PM>

You can set your prompt to to include seconds as well. This can be useful if you want to get a feel for how long individual commands that you run are taking to process.

$ PS1="\D{}> "
08:48:40 PM>

If you prefer the 24 hour format, use \A or \t.

$ PS1="\A> "
PS1="\t> "

NOTE: The > at the end of these prompts and the blank that follows are optional, but my preference. I like these characters separating my prompts from my commands.

Have trouble remembering what system you're logged into?

$ PS1='\h> '

Want to feel bad because it takes you so many commands to complete a task?

$ PS1='\#> '

Use ! if you want to see where you are with respect to your history buffer.

11> PS1='\!> '

If you can't remember where you are, the w setting will show your path relative to your home directory. The ~, of course, is your home itself.

PS1='\w '

When you need some cheering up:

$ PS1=':-) '

When you have amnesia or just want to be reminded what account you're logged into:

PS1="\u> "

You can expand this to show both the username and system name like this:

PS1="\u@\h "

$ PS1="\u@\h> "

Any of the following can be use din your prompt, though I suspect that few of us will hear the bell even if we use that character string in our prompts.

\a     an ASCII bell character (07)
\d     the date  in  "Weekday  Month  Date"  format (e.g.,
"Tue May 26") \e an ASCII escape character (033) \h the hostname up to the first `.' \H the hostname \j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name \n newline \r carriage return \s the name of the shell, the basename of $0 (the
portion following the final slash) \t the current time in 24-hour HH:MM:SS format \T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format \u the username of the current user \v the version of bash (e.g., 2.00) \V the release of bash, version + patch level (e.g.,
2.00.0) \w the current working directory \W the basename of the current working directory \! the history number of this command \# the command number of this command \$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn \\ a backslash \[ begin a sequence of non-printing characters, which could
be used to embed a terminal control sequence into the prompt \] end a sequence of non-printing characters

You can also put other information into your prompt, but there's a catch. Unless you're using one of the bash prompt escape sequences (like \d and \@), the prompt is set up when you define PS1 and not updated. Set PS1 to the output of the date command and your prompt will display that date and time until you log off.

PS1="`date`> "
Sun Mar  1 18:45:32 EST 2015>

If you're feeling blue, you can even change the color of your prompt:

$ PS1="\e[34m\]\u@\h "

Many other colors are possible:

  • 30m: Black
  • 31m: Red
  • 32m: Green
  • 33m: Yellow
  • 34m: Blue
  • 35m: Purple
  • 36m: Cyan
  • 7m: White

You can even use multiple colors in the same prompt by doing something like this:

PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;35m\]\w\[\e[m\]> '

Note the \[ and \] around the color codes. You can also alter the look of the fonts that you use as in the examples shown below.

0=regular 0;31m
1=bold 1:35m
4=underline 4:32m

Here's a cute little trick that involves your prompt. You can put the current time in right hand corner of your terminal window and change your prompt to a "> " with a setting like this:

PS1="> \[\033[s\]\[\033[1;\$((COLUMNS-4))f\]\$(date +%H:%M)\[\033[u\]"

Or, if you prefer, display the day of week:

PS1="> \[\033[s\]\[\033[1;\$((COLUMNS-8))f\]\$(date +%A)\[\033[u\]"

System prompts aren't the most exciting part of working on the Unix command line, but they provide helpful information if you take the time to set them up.

Copyright © 2015 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022