Home of Rob
Menu
Change Linux shell appearance
When I reload Linux, one of the things I like to customise is the appearance of the command prompt. When you first run the shell (or command prompt, for those more familiar with windows. The DOS prompt in windows is similar to the Linux shell, only it's far, far more powerful in Linux), you'll see something like the following:
~>
And this allows a place for you to start typing and running commands. It's possible to customise both the text that is written here, and also the colour in which it's written. The settings are saved in the environment variable PS1, and you can view the current value of yours by typing the following:
echo $PS1
The value is stored in your ~/.bashrc file, but for temporary changes you can alter the value by typing the following:
PS1="..."
(changing the ... for the particular settings you want to use). Here's an example of a colourful prompt:
PS1="\[\e[1;34m\]\u@\h:\[\e[1;31m\]\w\[\e[1;33m\]>\[\e[1;37m\] "
I'll go through now and explain each step in building up this prompt...
Changing the Text
You can alter the text that's presented in the prompt using either the literal string you wish to use, or with various escape sequences. For example, to insert the current user you can use \u. For the current time you can use \d. Here's the full set of escape sequences used in the bash prompt:
| Character | Meaning |
| a | ASCII bell character |
| d | Date (Wed Oct 3) |
| D{format} | Date (format is passed to strftime) |
| e | ASCII escape character |
| h | Hostname up to first '.' |
| H | Hostname |
| j | Number of jobs currently running |
| l | Base name of terminal device |
| n | Newline |
| r | Carriage Return |
| s | Name of the shell |
| t | Current time (HH:MM:ss - 24-hour) |
| T | Current time (HH:MM:ss - 12-hour) |
| @ | Current time (HH:MM am/pm format) |
| A | Current time (HH:MM - 24 hour format) |
| u | Current user |
| v | Bash version |
| V | Bash release version |
| w | Current working directory |
| W | Basename of current working directory |
| ! | History number of this command |
| # | Command number of this command |
| $ | If effective UID is 0, then '#', else '$' |
| nnn | Insert octal number nnn |
| \ | Backslash |
I like to display the username, machine name and current working directory at the prompt, so let's start with this:
PS1="\u@\h:\w> "
Note the space at the end, after the '>' but before the closing double quote. This ensures that the commands that are typed in are properly separated from the prompt itself.
As you can see from the table above, there are plenty of other things that you can put into your prompt. Just use the appropriate escape sequence.
Next, I want to add some colour to the prompt...
Adding Colour to the Prompt
It's this part that makes the PS1 value look a bit intimidating and complicated at first site. It's actually straight-forward - insert a colour code and then all text that follows will be in this new colour, until a new code is encountered. The trouble arrives because we need to escape part of the sequence. Oh, and the colour codes are in a strange format:
[e[m]
I'm using ubuntu and need to put them in with backslashes, like this \[\e[m\]. Here are the codes you can use:
| Colour (Text and background) |
Code | Colour (Text 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 | |||
Note: there's an option for enabling bold text. This is only relevant for the text (as opposed to the background) colour. Therefore, the first colomn of colours here can be applied to both foreground and background, whereas the second column is for text only (these colours are bold). For the first column of colours, substitute x=3 to specify the text, and x=4 for the background.
I'd like a blue username, red working directory and then a yellow prompt character, which can be achieved with the following:
PS1="\[\e[1;34m\]\u@\h:\[\e[1;31m\]\w\[\e[1;33m\]> "
This works fine, except now the text I type in is also yellow! I need to revert the colour back to white so that it looks ok. This is done by adding a new colour to the end of the prompt:
PS1="\[\e[1;34m\]\u@\h:\[\e[1;31m\]\w\[\e[1;33m\]>\[\e[1;37m\] "
And there we have it - a pretty prompt.