How do I get a colored bash?
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.
Related videos on Youtube
daniel451
Updated on September 18, 2022Comments
-
daniel451 over 1 year
How can I get the bash to look colored like this?
-
Admin over 6 yearsdaniel451 how come you have answered for your own question within the same timings.
-
Admin over 6 years@kva Answering your own question at the same time as posting is encouraged across the Stack Exchange network.
-
Admin over 5 yearsRelated: askubuntu.com/questions/123268/…. It explains how individual parts of the prompt can be coloured differently.
-
-
Scott Goodgame over 9 yearsYou might want to check out liquid prompt. It is a really info packed little prompt. google.com/…
-
Hannu over 9 years
-
Hannu over 9 years
-
Hannu over 9 yearsibm.com/developerworks/linux/library/l-tip-prompt more of the direct number crunching.
-
Hannu over 9 years
man 5 terminfo
shows whattput
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 about 8 yearsPerfect color choice!
-
James Hirschorn over 7 yearsDoesn't exactly answer the question, but I like this result better (less distracting).
-
To Kra over 7 yearsI think it does answer. why not ?
-
James Hirschorn over 7 yearsHere is what mine looks like after your method: !2016-10-25 16:12:15.png Not exactly as in the question.
-
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 over 6 yearsIs
force_color_prompt=yes
the intended way of enabling colors? To me forcing sounds like a workaround. -
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 withexport PS1
, then do as i mentioned. thx -
Jaakko over 6 yearsI 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 over 6 yearsi 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 over 6 yearsAn online GUI, Easy Bash PS1 Generator
-
gman over 4 yearsIt 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 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 about 4 yearsI 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 over 3 years+1 for the link to the color chart :)
-
C.K. about 3 yearsThis is exactly what I want! It's amazing!
-
Lethargos over 2 yearsGreat tip! Thanks!
-
Hannes Schneidermayer over 2 yearsOkay, this is really sexy. Handy to color code every server differently, for more stonks.