Making Your Linux Bash Command Prompt Useful
A quick guide for personalizing your Linux command prompt by NicholasSolutions
The Problem
I just switched hosts the other day. The first time I logged in through SSH, I realized my command prompt was pretty ugly -- it took up almost half the screen (and no, for you security nuts out there, that is not my real username):
nicholas55@nicholassolutions.com$
I suppose this is a bit better than some of the other default prompts that hosts provide, but it could be a lot better. If you didn't know this already, the command prompt (literally, the text before you type a command) can actually be very useful, and it is easy to customize so it contains information you want. In this tutorial I'll show you how to customize your bash shell prompt (bash is the shell most commonly used when logging in through an SSH session. On BSD and Max OS X, the default shell is tcsh. Customizing these shells follows the same general principles but it's a tiny bit different and I won't be covering those differences here).
The Solution: Customize your command prompt
The text for the prompt is held in the environment variable PS1 ("Prompt String 1"), and all you need to do to change
the prompt is update the variable. I prefer to have the end of my prompt be the "greater than" sign, >. I usually know
what user I am logged-in as, and where I am logged-in to, and since my host name is fairly long that makes the default
setup a real nusance. In fact just having a > and nothing else would be an improvement. To do that, all I need to do at
the command prompt is type PS1="> " (follow along with me in your own SSH session; none of the changes are
permanent and everything will go back to normal the next time you log in):
nicholas55@nicholassolutions.com$ PS1="> "
>But we can do better. It's always nice to know what directory you're currently in without having to type pwd all the time.
Using the w control sequence will display your path starting from your user base path (your home directory):
> PS1="PS1="w> "
~> cd www
~/www>There are many other control sequences. Here's the full list from the the man page for the bash shell
(type man bash at
your command prompt to look through this --and there is plenty there to look at-- and type q
to quit back to your shell).
a an ASCII bell character (07)
d the date in "Weekday Month Date" format (e.g., "Tue May
26")
D{format}
the format is passed to strftime(3) and the result is
inserted into the prompt string; an empty format results
in a locale-specific time representation. The braces are
required
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
A the current time in 24-hour HH:MM format
u the username of the current user
v the version of bash (e.g., 2.00)
V the release of bash, version + patchelvel (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
Some of these are less useful than others, but I figured I'd include them all. The last two are not actually "characters" themselves -- you wrap them around unprintable control characters, similar to the opening and closing of an HTML tag. For example, to make the most annoying command prompt in the world, you might type
~/www> PS1="***Beep***[a] "
***Beep*** cd ..
***Beep***
The a is the ASCII bell sound, so the terminal beeps as if
there is an error after every command is entered. This can make a good practical joke, not that I'm advocating that
sort of thing ;-)
Adding some color
You can do less annoying things as well. For example, you can change the font
and background colors using the escape character. The format is [e[COLORCODEm]
where COLORCODE is a code for
the color you want to switch to, as given in the table below. [e[0m] switches the color to nothing; that is,
whatever it was before you modified it. To specify the background and foreground at the same time, you can use
[e[COLORCODE1;COLORCODE2m] format,
that is, separate the two codes with a semicolon. However, it usually works
better to specify them separately as [e[COLORCODE1m][e[COLORCODE2m]
| Color [both] | Code | Color [foreground only] | Code | |||
|---|---|---|---|---|---|---|
| Black | 0;x0 |
Dark Gray | 1;30 |
|||
| Red | 0;x1 |
Light Red | 1;31 |
|||
| Green | 0;x2 |
Light Green | 1;32 |
|||
| Brown | 0;x3 |
Yellow | 1;33 |
|||
| Blue | 0;x4 |
Light Blue | 1;34 |
|||
| Purple | 0;x5 |
Light Purple | 1;35 |
|||
| Teal | 0;x3 |
Cyan | 1;33 |
|||
| Light Gray | 0;x7 |
White | 1;37 |
|||
Strictly speaking, the 1 or 0 preceding each code are not actually part of the color code - they are 'state' modifiers. In the console, 1 turns on the 'light' colors. In an xterm window, it will make the text bold. (The background cannot be 'bold' (it's the background after all), which is why only the colors on the left side of the table may be used as background colors).
Let's make our command prompt red (and stop it from beeping, damnit!):
***Beep*** PS1="[e[0;31m]w>[e[0m] "
~>
Or how about white text on a blue background:
~> PS1="[e[0;37;0;44m]w>[e[0m] "
~> pwd
/home/nicholas55
~>
In fact, if we don't set the color back to normal, we can make the color change take effect on the whole window:
~> PS1="[e[0;37;0;44m]w> "
~> pwd
/home/nicholas55
~>Here's the command prompt I like to use:
~> PS1="[e[0;40m][e[1;33m][A] w> [e[0;37m]"[21:43] ~> pwd
/home/nicholas55
[21:43] ~>
The prompt is yellow, and the text after it is white. I don't turn colors off, because in case I am working from a computer where the default SSH setup is to have a white terminal background, I want to override it -- it's hard on the eyes-- and if I have a black background, I need the white text on top (there's another good practical joke in there if you think like I do ;-) ). If I turned either off, I could get some funky results.
"But wait! I don't want to do that every time I log in!"
Once you get your prompt how you like it, you have to tell your machine to set it up that way every time you log in,
because the environment variables you set during your session are reverted back to the defaults after you log out.
Lucky for us, all we need to do is add the code (preceeded by the export command) we used to set PS1 to our .bash_profile file
(note that the file name
does start with a dot -- that's not a typo) in our home directory.
You may already have this file: if so, just add the line; if not, create it and stick the line in. If you have a
file called .bashrc, you can use that instead (no need to create a new .bash_profile). As you become
more familiar with the shell, you'll probably add to your bash config file quite a bit. I've got a line
like this in my .bash_profile
export PS1="[e[0;40m][e[1;33m][A] w> [e[0;37m]"

