Get the highlights in your inbox every week.
Bash prompt tips and tricks
Bash prompt tips and tricks
Here are a few hidden treasures you can use to customize your Bash prompt.

Anyone who has started a terminal in Linux is familiar with the default Bash prompt:
[user@$host ~]$
But did you know is that this is completely customizable and can contain some very useful information? Here are a few hidden treasures you can use to customize your Bash prompt.
How is the Bash prompt set?
The Bash prompt is set by the environment variable PS1 (Prompt String 1), which is used for interactive shell prompts. There is also a PS2 variable, which is used when more input is required to complete a Bash command.
[dneary@dhcp-41-137 ~]$ export PS1="[Linux Rulez]$ "
[Linux Rulez] export PS2="... "
[Linux Rulez] if true; then
... echo "Success!"
... fi
Success!
Where is the value of PS1 set?
PS1 is a regular environment variable.
The system default value is set in /etc/bashrc. On my system, the default prompt is set with this line:
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
This tests whether the value of PS1 is \s-\v$ (the system default value), and if it is, it sets PS1 to the value [\u@\h \W]\\$.
If you want to see a custom prompt, however, you should not be editing /etc/bashrc. You should instead add it to .bashrc in your Home directory.
What do \u, \h, \W, \s, and \v mean?
In the PROMPTING section of man bash, you can find a description of all the special characters in PS1 and PS2. The following are the default options:
- \u: Username
- \h: Short hostname
- \W: Basename of the current working directory (~ for home, the end of the current directory elsewhere)
- \s: Shell name (bash or sh, depending on how the shell is called)
- \v: The shell's version
What other special strings can I use in the prompts?
There are a number of special strings that can be useful.
- \d: Expands to the date in the format "Tue Jun 27"
- \D{fmt}: Allows custom date formats—see man strftime for the available options
- \D{%c}: Gives the date and time in the current locale
- \n: Include a new line (see multi-line prompts below)
- \w: The full path of the current working directory
- \H: The full hostname for the current machine
- \!: History number—you can run any previous command with its history number by using the shell history event designator ! followed by the number for the specific command you are interested in. (Using Linux history is yet another tutorial...)
There are many other special characters—you can see the full list in the PROMPTING section of the Bash man page.
Multi-line prompts
If you use longer prompts (say if you include \H or \w or a full date-time), you may want to break things over two lines. Here is an example of a multi-line prompt, with the date, time, and current working directory on one line, and username @hostname on the second line:
PS1="\D{%c} \w\n[\u@\H]$ "
Are there any other interesting things I can do?
One thing people occasionally do is create colorful prompts. While I find them annoying and distracting, you may like them. For example, to change the date-time above to display in red text, the directory in cyan, and your username on a yellow background, you could try this:
PS1="\[\e[31m\]\D{%c}\[\e[0m\]
\[\e[36m\]\w\[\e[0m\]\n[\[\e[1;43m\]\u\[\e[0m\]@\H]$ "
To dissect this:
- \[..\] declares some non-printed characters
- \e[.. is an escape character. What follows is a special escape sequence to change the color (or other characteristic) in the terminal
- 31m is red text (41m would be a red background)
- 36m is cyan text
- 1;43m declares a yellow background (1;33m would be yellow text)
- \[\e[0m\] at the end resets the colors to the terminal defaults
You can find more colors and tips in the Bash prompt HOWTO. You can even make text inverted or blinking! Why on earth anyone would want to do this, I don't know. But you can!
What are your favorite Bash prompt customizations? And which ones have you seen that drive you crazy? Let me know in the comments.
8 Comments, Register or Log in to post a comment.
Good, useful tips here. Especially the ones about special strings and multi-line prompts -- I keep forgetting those ones ...
Would have been nice to see screenshots of some of these changes. Also, something like:
"$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
is absolutely horrific looking. My God... what a mess of characters. Miss the DOS/4DOS prompt too often when having to deal some Linux syntax stuff. You could do so much with 4dos without having to read 300 page manuals to do the same on Linux. It's insane. Oh well...
Thanks for the feedback John! I agree, some shell constructs are very confusing and need explanation - this one in particular is of the form:
[ test ] && action
[ ... ] tests whether an action returns true or false.
a && b executes a, and if it is true, executes b (and evaluated to true or false, depending on whether both commands return true (or success) or not.
So "[ test ] && action" will execute the action only if the test returns true. It is equivalent to:
if [ test ]; then
action;
fi
Maybe shell built-ins should be the next article I do?
Thanks again!
Dave.
4DOS/4OS2/4CMD... used 'em all! They were indeed FAR ahead of their time. BTW, there are nice wrappers for generating prompts from high-level descriptions...
I really like the Bash-Beautify setup by Chris Albrecht:
https://github.com/KeyboardCowboy/Bash-Beautify/blob/master/.bash_beautify
When you're in a version-controlled directory, it includes the VCS information (e.g. the git branch and status), which is really handy if you do development.
Thanks! That is awesome!
One thing I did not touch on, which this script uses, is how to execute an external command in PS1.
PS1="$(cmd)"
Thanks,
Dave.
An easy drag and drop interface to build your own .bashrc/PS1 configuration
http://bashrcgenerator.com/
've phun!
I really love liquidprompt: https://github.com/nojhan/liquidprompt
Sounds similiar to Bash Beatuifier, but covers more shells.