71

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

colored-bash

  • daniel451 how come you have answered for your own question within the same timings. – kva Nov 16 '17 at 14:20
  • 4
    @kva Answering your own question at the same time as posting is encouraged across the Stack Exchange network. – CJ Dennis Jan 19 '18 at 4:38
  • Related: askubuntu.com/questions/123268/…. It explains how individual parts of the prompt can be coloured differently. – FreezingFire Dec 1 '18 at 10:48
107

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

#force_color_prompt=yes

to be:

force_color_prompt=yes

save then execute source ~/.bashrc

  • Doesn't exactly answer the question, but I like this result better (less distracting). – James Hirschorn Oct 1 '16 at 18:27
  • I think it does answer. why not ? – To Kra Oct 25 '16 at 6:14
  • Here is what mine looks like after your method: !2016-10-25 16:12:15.png Not exactly as in the question. – James Hirschorn Oct 25 '16 at 20:19
  • @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 – To Kra Jan 5 '17 at 10:12
  • Is force_color_prompt=yes the intended way of enabling colors? To me forcing sounds like a workaround. – Jaakko Jan 14 '18 at 11:09
47

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:

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.

-2

@daniel451 Try this in your $HOME/.bashrc

PS1='\[\033[6;31m\]-daniel\[\033[5;51m\]@\[\033[3;33m\]UbuntuUltrabook\[\033[3;35m\]\$\[\033[0;34m\]\a\[\033[0m\] '

you will get as you expected.

  • 2
    How is this an improvement over daniel451's own answer? You have even hardcoded the username and hostname! – muru Nov 16 '17 at 6:28

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.