13

There are a number of tutorials/posts online (e.g. http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/) about how to customize your prompt in bash by setting PS1. Is there a way to customize the color/bolding of the command I type?

In other words, as an example lets say that I wanted a green prompt, a bold blue command, and then output in whatever colors would be default. In the example below, I already know how to make "joe>" show as green, blue, bold, whatever by setting PS1. But is there a way I can I make git status show as bold for example?

joe> git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
...
  • If you want a really good shell you should use zsh,what you want to achieve there would be very simple with preexec() { printf '\e[0m' } – bollovan Sep 18 '11 at 9:32
  • 3
    @bollovan - 'Really good' is a bit subjective, I find it really detestable because of its bloat and POSIX noncompliance. But, whatever floats your boat. – Chris Down Sep 18 '11 at 12:38
  • @ChrisDown bloat floats your boat - never heard it, quite amusing. – n611x007 Mar 14 '13 at 13:14
12

You'd have to leave a colour trailing after the PS1 (start it after > in your example), and then use the bash DEBUG trap to clear the colour before your command was run (but after you press enter in your shell. Try something like this:

shopt -s extdebug
trap "tput sgr0" DEBUG
  • for someone stranger to escape codes: would this preserve the original coloring of the command (eg. would ls --color=always be colored)? – n611x007 Mar 14 '13 at 13:16
  • @naxa - Yes, as this explicitly sets colour codes (except for colours that are colored in the default terminal colour, which will only take effect after the first colour). – Chris Down Mar 15 '13 at 1:13
  • This trick seems to interfere with tab completion (prints weird things like ^[(B^[[m). It happens only on some systems, though: I tested this on two systems, both running Ubuntu 12.04 LTS, and only one of them had the interference. – musiphil Jul 15 '13 at 18:47
4

For completeness, here's how to set a prompt color and the command line input color in bash. Put these lines in your .bashrc.

PS1='\[\e[1;34m\]\w\$ \[\e[0;32m\]'
trap 'printf \\e[0m' DEBUG

The DEBUG trap, as suggested by Chris Down, is used to reset the text attributes. It's meant for debugging, but it's also useful to execute code before executing the command typed by the user.

In the prompt setting:

  • \[…\] is used around escape sequences to tell bash that they don't use up any screen estate (without them, the screen would become garbled when bash needs to redraw the prompt).
  • \e[…m where is a number, or more generally a sequence of numbers separated by semicolons, are escape sequences sent to the terminal to affect the color and other attributes of the following text. For example, 1;34 sets bold blue; 0;32 resets all attributes then switches the color to green. Adjust to taste.
  • \w\$ print the current directory and $ or #. You can of course change this for whatever you like, and insert text attribute changes in between if you like.
  • There's a final attribute change at the end, which sets the color of the text input.
0

I am just learning about BASH commands and I have been sitting here playing with the color commands in my version. I figured out this solution:

Using this command:

PS1='\[\e[0:36m\]\h>\[\e[0;31m\]'

Deconstruct: PS1= :starts the PS1 command

' ' :contain the command in single quotes (so BASH doesn't think it's a command for it)

\ :each individual command starts with this

[ ] : contain each command

\e (could also be \033 an ASCII escape character): starts a color command

The color command precedes the thing it applies to.

[\e[0;36m] : cyan

\h : host name

'>' : printed after the host name

[\e[0;31m] : red

Apparently, the red command at the end makes the input red. Just change the colors to your liking and that is what you are looking for.

Also, this site helped me a little:

https://www.linuxnix.com/linuxunix-shell-ps1-prompt-explained-in-detail/

My Version: GNU bash, v. 4.2.46

Take Care,

Weasel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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