Page 1 of 6 123 ... LastLast
Results 1 to 10 of 55

Thread: HOWTO: Customizing bash prompt

  1. #1
    Join Date
    Apr 2006
    Location
    Bjelovar, Croatia
    Beans
    128

    HOWTO: Customizing bash prompt

    The information in this thread has been moved to https://help.ubuntu.com/community/CustomizingBashPrompt

    A thread for discussion of the wiki page only can be found here http://ubuntuforums.org/showthread.php?t=2012407

    Thread closed.


    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 Attached Images
    Last edited by Elfy; June 29th, 2012 at 09:12 AM. Reason: Updated for new .bashrc
    You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield

  2. #2
    Join Date
    Jun 2006
    Location
    Buenos Aires, Argentina
    Beans
    686
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: HOWTO: Customizing bash prompt

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

    Thanks.

  3. #3
    Join Date
    May 2007
    Location
    Zagreb, Croatia
    Beans
    10
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: HOWTO: Customizing bash prompt

    this is great, when I get home, I'll try it

  4. #4
    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.
    Last edited by MiCovran; November 17th, 2007 at 05:17 PM.
    You know what a "diet" is, don't you? It's "die" with a "t", that's what it is! -- Garfield

  5. #5
    Join Date
    Dec 2007
    Location
    Sweden
    Beans
    10
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: HOWTO: Customizing bash prompt

    Good stuff, now I can finally see where my latest compile results start.

  6. #6
    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

  7. #7
    Join Date
    Aug 2008
    Beans
    56
    Distro
    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

  8. #8
    Join Date
    Jul 2008
    Beans
    2

    Re: HOWTO: Customizing bash prompt

    wat a great tutorial..do u have any customization??..plz share..

  9. #9
    Join Date
    Sep 2006
    Beans
    3,170
    Distro
    Ubuntu Karmic Koala (testing)

    Re: HOWTO: Customizing bash prompt

    Great howto, thanks!

  10. #10
    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

Page 1 of 6 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •