How do I get a colored bash?

245,658

Solution 1

I came up with this solution:

  • open ~/.bashrc in an editor
  • copy this and add it at the end of .bashrc file:

    PS1='\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] '
    
  • save the file and restart bashrc:

    source ~/.bashrc
    

For a full list of available colors and further options look up these links:

Solution 2

Open ~/.bashrc in text editor and uncomment line:

#force_color_prompt=yes

to be:

force_color_prompt=yes

save then execute source ~/.bashrc

Solution 3

A version that is a bit more 'general' - should work with a varied environment:
(depends on terminfo)

Insert this in your $HOME/.bashrc:

function fgtab {
  echo "tput setf/setb - Foreground/Background table"
  for f in {0..7}; do
    for b in {0..7}; do
      echo -en "$(tput setf $f)$(tput setb $b) $f/$b "
    done
    echo -e "$(tput sgr 0)"
  done
}

# The prompt in a somewhat Terminal -type independent manner:
cname="$(tput setf 3)"
csgn="$(tput setf 4)"
chost="$(tput setf 2)"
cw="$(tput setf 6)"
crst="$(tput sgr 0)"
PS1="\[${cname}\]\u\[${csgn}\]@\[${chost}\]\h:\[${cw}\]\w\[${csgn}\]\$\[${crst}\] "

Then execute source ~/.bashrc.

After that, fgtab will display a color table with numbers. Those numbers are for tput setf n and tput setb n where 'n' is the number, 'f' stands for 'foreground' and 'b' stands for 'background' color.

tput sgr 0 will reset foreground and background colors to default.

And as you can see, changing the colors used for the prompt becomes really easy (just edit the same number in $HOME/.bashrc as you wish).

Add an $(tput setb n) in $cname if you wish to have ALL of the prompt with background n.

Share:
245,658

Related videos on Youtube

daniel451
Author by

daniel451

Updated on September 18, 2022

Comments

  • daniel451
    daniel451 over 1 year

    How can I get the bash to look colored like this?

    colored-bash

    • Admin
      Admin over 6 years
      daniel451 how come you have answered for your own question within the same timings.
    • Admin
      Admin over 6 years
      @kva Answering your own question at the same time as posting is encouraged across the Stack Exchange network.
    • Admin
      Admin over 5 years
      Related: askubuntu.com/questions/123268/…. It explains how individual parts of the prompt can be coloured differently.
  • Scott Goodgame
    Scott Goodgame over 9 years
    You might want to check out liquid prompt. It is a really info packed little prompt. google.com/…
  • Hannu
    Hannu over 9 years
  • Hannu
    Hannu over 9 years
  • Hannu
    Hannu over 9 years
    ibm.com/developerworks/linux/library/l-tip-prompt more of the direct number crunching.
  • Hannu
    Hannu over 9 years
    man 5 terminfo shows what tput can be used with, it is a bit technical though so "take heed" - but a bit of guessing and testing may actually let you get it working.
  • Puck
    Puck about 8 years
    Perfect color choice!
  • James Hirschorn
    James Hirschorn over 7 years
    Doesn't exactly answer the question, but I like this result better (less distracting).
  • To Kra
    To Kra over 7 years
    I think it does answer. why not ?
  • James Hirschorn
    James Hirschorn over 7 years
    Here is what mine looks like after your method: !2016-10-25 16:12:15.png Not exactly as in the question.
  • To Kra
    To Kra over 7 years
    @JamesHirschorn force_color only enabled colors, you can always manage how coloring will look like with PS1 variable see my example in one of my scripts github.com/to-kra/dotfiles/blob/master/scripts/… which also enabling git status... you can have your own PS1 exported in user profile, and make fancy colors and format you like. thx
  • Jaakko
    Jaakko over 6 years
    Is force_color_prompt=yes the intended way of enabling colors? To me forcing sounds like a workaround.
  • To Kra
    To Kra over 6 years
    @Jaakko I posted this long time ago, and its quickest way on debian/ubuntu, and its easiest way. I am now using export PS1 but its only because I wanted to modify appearance of default settings. If you dont want to spend too much time with export PS1, then do as i mentioned. thx
  • Jaakko
    Jaakko over 6 years
    I don't know how the colors are intended to be enabled. I'm just thinking that if there was some other way than "force" for the used console emulator to tell the .bashrc that it can handle colors. Exporting PS1 also feels a bit cumbersome, I'd be totally fine with default colors.
  • To Kra
    To Kra over 6 years
    i got no idea, i alway used force. in answer bellow, which is marked as GreenTick, there is this PS1 way i mentioned. Dunno how else it can be enabled.
  • Jim Fred
    Jim Fred over 6 years
    An online GUI, Easy Bash PS1 Generator
  • gman
    gman over 4 years
    It is good to add the details of the answer from the link in your answer in case the link ever goes down.
  • Rémy Hosseinkhan Boucher
    Rémy Hosseinkhan Boucher about 4 years
    @Jaakko I like your interrogation. The bashrc file claims the following: # uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yes What do you think about that ?
  • Jaakko
    Jaakko about 4 years
    I hope I didn't sound disrespectful with my comments, I'm just trying to understand how it was meant to work. For example, above those lines you mention, there's a different way of enabling colors, xterm-color|*-256color) color_prompt=yes;; Which makes me think that the colors could automatically enable if you had correct value in TERM variable.
  • bitsmack
    bitsmack over 3 years
    +1 for the link to the color chart :)
  • C.K.
    C.K. about 3 years
    This is exactly what I want! It's amazing!
  • Lethargos
    Lethargos over 2 years
    Great tip! Thanks!
  • Hannes Schneidermayer
    Hannes Schneidermayer over 2 years
    Okay, this is really sexy. Handy to color code every server differently, for more stonks.