Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old November 16th, 2007   #1
MiCovran
A Carafe of Ubuntu
 
MiCovran's Avatar
 
Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
HOWTO: Customizing bash prompt

If you have difficulties using the terminal because the prompt isn't visible enough or you would simply like it to look nicer, this howto is for you. It should work on any distribution if you use bash shell, just don't expect your existing .bashrc file to look like Ubuntu's. I've attached a before and after picture of my terminal as an example of what you could do.

So let's get started. Fire up your terminal and stay in your home directory.


1. Backup

First, let's backup our .bashrc file since we're about to make some changes to it:
Code:
cp .bashrc .bashrc-backup
The .bashrc file tells bash what to do when terminal is started, including how to show prompt.


2. Preparing the .bashrc

Use your favorite text editor to edit the original .bashrc file, eg.:
Code:
gedit .bashrc
Find this part:
Code:
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Bash reads PS1 variable to see how to show primary prompt and PS2 for a secondary prompt (used when writing multi-line commands). In this "if" block, the first PS1 is a prompt which will be shown when color is turned on, and the second one (after "else") is being used when no color is desired. As you can see, color codes are quite tiring to use. Imagine you have to write things like \[\033[01;32m\] all over again. It's horrible. Fortunetly, bash lets us define variables that make our life easier. So paste this code above that "if" block:
Code:
# ANSI color codes
RS="\[\033[0m\]"    # reset
HC="\[\033[1m\]"    # hicolor
UL="\[\033[4m\]"    # underline
INV="\[\033[7m\]"   # inverse background and foreground
FBLK="\[\033[30m\]" # foreground black
FRED="\[\033[31m\]" # foreground red
FGRN="\[\033[32m\]" # foreground green
FYEL="\[\033[33m\]" # foreground yellow
FBLE="\[\033[34m\]" # foreground blue
FMAG="\[\033[35m\]" # foreground magenta
FCYN="\[\033[36m\]" # foreground cyan
FWHT="\[\033[37m\]" # foreground white
BBLK="\[\033[40m\]" # background black
BRED="\[\033[41m\]" # background red
BGRN="\[\033[42m\]" # background green
BYEL="\[\033[43m\]" # background yellow
BBLE="\[\033[44m\]" # background blue
BMAG="\[\033[45m\]" # background magenta
BCYN="\[\033[46m\]" # background cyan
BWHT="\[\033[47m\]" # background white
Now you don't have to write all that ANSI code over and over again. For instance, you can just use $FBLE to set foreground (text) to blue, instead of \[\033[34m\].


3. Setting the prompt without color

Now that you have this prepared, you can design your own prompt. Let's start with the one without color for simplicity (it's the one just below the "else" line). First, comment in the default PS1 that's already there to ignore it (put a # in front of the line), and then define your own PS1 and PS2 just below it.

I'll explain how I did mine. This is how they are defined:
Code:
PS1="[ ${debian_chroot:+($debian_chroot)}\u: \w ]\\$ "
PS2="> "
Secondary prompt is simple. It shows just >.
However, the primary one is a little bit more complicated. It contains some special characters for additional info about a session. You can find a list of those special characters in bash's man page under section PROMPTING:
Code:
man bash
I used \u (username), \w (current working directory) and, of course, \\$ (the $ or # symbol, depending on your privileges). You don't need to worry about what "${debian_chroot:+($debian_chroot)}" means, just always copy-paste it in front of "/u". So my primary prompt will be shown like this (when I'm in my home directory): [ penguin: ~ ]$


4. Setting the prompt with color

If you want to use a colored prompt, you must find the line in your .bashrc that says #force_color_prompt=yes and remove # from it. This means that bash will from now on prefer what is defined above the "else" line. Comment in the old PS1 that's located there just like you did before, and then copy-paste your PS1 and PS2 from below "else" (colorless versions). You now have the base layout and you just need to add some color to it.

The simplest way to deal with it is by inserting variables from that big chunk of code you pasted before. First put a $RS at the end of both PS1 and PS2 so you don't change formating of other text in the terminal. All ANSI color codes change only the text behind them, and you can only turn off $HC, $UL and $INV codes by using $RS. Here's how I added color to my prompt:
Code:
PS1="$HC$FYEL[ $FBLE${debian_chroot:+($debian_chroot)}\u$FYEL: $FBLE\w $FYEL]\\$ $RS"
PS2="$HC$FYEL> $RS"
I used hicolor at the beginning and left it on until that last $RS. Then I just switched between colors I wanted (blue and yellow). So I got this prompt: [ penguin: ~ ]$

When you want to see what you've done, just save changes to .bashrc file, start another terminal and enjoy your new prompt. If you want to see how your secondary prompt looks like, enter just \ as a command in the terminal.

Tip: If you're using gnome-terminal and want to fine tune your color selection, right click the terminal, choose Edit Current Profile... and go to Colors tab. Note that these changes effect the complete terminal, not just prompt. I don't know how to do this for any other terminal emulators, I'm a GNOME man.


5. Reverting to old settings

If for some reason you wish to return the old prompt or if you broke your .bashrc, the easiest way is to just recover old .bashrc file you backed up. Run a terminal and use:
Code:
cp .bashrc-backup .bashrc
Voila! Everything is back to normal.


For the end

I tried to make this howto as simple to understand as I could. If you don't understand or don't know how to do something, if you have a suggestion or think I made a mistake somewhere, feel free to contact me. This is the first howto I wrote so any feedback is appreciated.
This is just my small way to say thanks to people in this forum who helped me a lot, so ... thanks.
Attached Images
File Type: png before.png (70.8 KB, 869 views)
File Type: png after.png (70.8 KB, 1017 views)
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield

Last edited by MiCovran; May 18th, 2009 at 07:18 AM.. Reason: Updated for new .bashrc
MiCovran is offline   Reply With Quote
Old November 16th, 2007   #2
tzulberti
Skinny Soy Caramel Ubuntu
 
tzulberti's Avatar
 
Join Date: Jun 2006
Location: Buenos Aires, Argentina
Beans: 686
Kubuntu 8.04 Hardy Heron
Send a message via Skype™ to tzulberti
Re: HOWTO: Customizing bash prompt

Great How-to.... I will try to customize it...

Thanks.
tzulberti is offline   Reply With Quote
Old November 17th, 2007   #3
mfolnovic
First Cup of Ubuntu
 
mfolnovic's Avatar
 
Join Date: May 2007
Location: Zagreb, Croatia
Beans: 10
Ubuntu 7.04 Feisty Fawn
Send a message via ICQ to mfolnovic Send a message via AIM to mfolnovic Send a message via MSN to mfolnovic Send a message via Yahoo to mfolnovic Send a message via Skype™ to mfolnovic
Re: HOWTO: Customizing bash prompt

this is great, when I get home, I'll try it
__________________
Hammerfall is real music for programmers!!!
HAMMERFALL RULES!!!
mfolnovic is offline   Reply With Quote
Old November 17th, 2007   #4
MiCovran
A Carafe of Ubuntu
 
MiCovran's Avatar
 
Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
Re: HOWTO: Customizing bash prompt

Quote:
Originally Posted by tzulberti
I will try to customize it...
Quote:
Originally Posted by mfolnovic
when I get home, I'll try it
Let me know how it worked.

BTW, I felt like section 3 - setting the prompt - wasn't clear enough for people with no scripting or programming experience, so I rewrote it in a more "step-by-step" approach.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield

Last edited by MiCovran; November 17th, 2007 at 11:17 AM..
MiCovran is offline   Reply With Quote
Old April 15th, 2008   #5
Trezker
First Cup of Ubuntu
 
Join Date: Dec 2007
Location: Sweden
Beans: 10
Ubuntu 7.10 Gutsy Gibbon
Re: HOWTO: Customizing bash prompt

Good stuff, now I can finally see where my latest compile results start.
Trezker is offline   Reply With Quote
Old April 20th, 2008   #6
MiCovran
A Carafe of Ubuntu
 
MiCovran's Avatar
 
Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
Re: HOWTO: Customizing bash prompt

Quote:
Originally Posted by Trezker View Post
Good stuff, now I can finally see where my latest compile results start.
That's the primary reason why I got into it too.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield
MiCovran is offline   Reply With Quote
Old January 12th, 2009   #7
uvdevnull
Just Give Me the Beans!
 
uvdevnull's Avatar
 
Join Date: Aug 2008
Beans: 56
Ubuntu 9.04 Jaunty Jackalope
Re: HOWTO: Customizing bash prompt

Thanks for that MiCovran, very helpful!

Just a small correction... The ANSI color variables must be defined prior to the prompts that use them, otherwise it doesn't work, at least for me. So they can't be pasted at the end of file, but anywhere before the first attempted usage.

Thanks again,
uvdevnull
uvdevnull is offline   Reply With Quote
Old January 18th, 2009   #8
h711
First Cup of Ubuntu
 
Join Date: Jul 2008
Beans: 2
Re: HOWTO: Customizing bash prompt

wat a great tutorial..do u have any customization??..plz share..
h711 is offline   Reply With Quote
Old January 18th, 2009   #9
binbash
Skinny Extra Sweet Ubuntu
 
binbash's Avatar
 
Join Date: Sep 2006
Beans: 3,171
Ubuntu Karmic Koala (testing)
Re: HOWTO: Customizing bash prompt

Great howto, thanks!
binbash is offline   Reply With Quote
Old January 21st, 2009   #10
MiCovran
A Carafe of Ubuntu
 
MiCovran's Avatar
 
Join Date: Apr 2006
Location: Bjelovar, Croatia
Beans: 128
Re: HOWTO: Customizing bash prompt

Quote:
Originally Posted by uvdevnull View Post
The ANSI color variables must be defined prior to the prompts that use them
I did write what you told in the howto. I've put it in bold now so it will be easier to spot.

Quote:
Originally Posted by h711 View Post
wat a great tutorial..do u have any customization??..plz share..
It's actually quite simple to customize it, you just have to play a little bit.

For changing the colour of the prompt just play with all the different variables you pasted into your .bashrc file (it's that big chunk of code from the howto). For instance, if you want to change colour of text in some part of the prompt to green, first find the "foreground green" line. There you'll see that the variable name is "FGRN" (on the beginning of the line). Then just add "$FGRN" before the text you want to change (in the "PS1" variable).

However, if you want to change contents of the prompt, then take a look at the bash man file (also available at this page):
Code:
man bash
Find the "PROMPTING" section and there you'll see all the features that your prompt can have. When you find what you like, just put it in the "PS1" variable in your .bashrc file. So if you would like your prompt to show time, you could just add "\t" to the beginning of the "PS1" variable. And if you want to add some text or some brackets (or basically anything), you can just type it as it is.

As an example, PS1="$FGRN(My first bash prompt) \t $RS" would simply show "(My first bash prompt)" and current time after it. All in green, of course.
__________________
You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield
MiCovran is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:34 AM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry