1 Dec 2013

A nice looking and useful bash PS1 prompt

It's been a while since I blogged anything. Getting lazier. Anyways the folks who use Linux and/or Mac are familiar with Bash. It's the default shell in most of the Linux distributions and Mac. And the folks who use these operating systems also know the power of working in the terminal. And the prompt you see whenever you open the terminal is controlled by PS1. Most of the time the default prompt is, let's face it, boring. Today I'm gonna give you a nice, colorful, two lined prompt that also displays the exit status of the last command. Exit status means whether the command failed or succeeded.
The main advantage of the two lines is, you get a clear field to input your commands. The '$' variable ['#' for root] will turn red if the last command failed, Otherwise it remains the default white.
Here is the code to change your PS1 prompt,

export PS1='\[\e[01;32m\]\u \[\e[01;37m\]> \[\e[01;33m\]\w\n\[\e[$(((($?>0))*31))m\]\$\[\e[0m\] '

And how it will look in normal condition,


When the command succeeds,


The dreaded moment when your enter a wrong command. Notice the '$' has changed to red,


If you like this and want to change your prompt all you have to do is add the above mentioned code at the end of your '~/.bashrc' file and run the following command,

source .bashrc

This will only change the user's prompt, If you want to change the prompt of root then you will have to add the above code to '/etc/bash.bashrc' file.


No comments: