12

My default terminal color is gray, and that's fine.

My Bash prompt displays a bunch of colors, and this works fine:

PS1="${COLOR_RED}\u${COLOR_WHITE}@${COLOR_RED}${COMPUTERNAME} ${COLOR_BLUE}\w${GITPROMPT} ${COLOR_RESET}"

But the text I type in, at the end of the prompt, is gray. I want it to be white (ANSI code "[37m").

If I add a COLOR_WHITE at the end of the prompt, instead of the COLOR_RESET, then the default terminal color changes to white until it is reset. This makes a weird effect of some gray text, with some white text bleeding through at the top.

How can I change the "input text" color of the Bash prompt, to something other than the terminal default color?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dylanized
  • 3,765
  • 6
  • 32
  • 44
  • Might found useful [How to change the output color of echo in Linux](https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux) and [Bash Prompt Generator](http://ezprompt.net/). – Pablo Bianchi Jul 25 '18 at 15:43

4 Answers4

8

Simply add the following line:

export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "

Preview:

Enter image description here

These are my preferred colors. You can customize each part of the prompt's color by changing m codes (e.g., 34m) which are ANSI color codes.

List of ANSI color codes:

  • Black: 30m
  • Red: 31m
  • Green: 32m
  • Yellow: 33m
  • Blue: 34m
  • Purple: 35m
  • Cyan: 36m
  • White: 37m
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shayan Amani
  • 5,787
  • 1
  • 39
  • 40
2

Try this one. It is simpler:

export PS1="\e[0;32m\t \e[32;1m\u@\h:\e[0;36m\w\e[0m$ "
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dlink
  • 1,489
  • 17
  • 22
0

I would suggest changing your terminal emulator's settings.

It appears you are using iTerm2 (if you are on iTerm, I suggest looking at iTerm2), so:

SettingsProfilesYour ProfileColor. Under 'basic colors', adjust 'foreground'.

For just changing the color of the input text, in Z shell (zsh) you could use a

preexec () { echo -ne "\e[0m" }

Source 1

I have found a hack-ish way to try this with Bash:

Not natively, but it can be hacked up using the DEBUG trap. This code sets up preexec and precmd functions similar to zsh. The command line is passed as a single argument to preexec.

Here is a simplified version of the code to set up a precmd function that is executed before running each command.

preexec () { :; }
preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return  # do nothing if completing
    local this_command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g");
    preexec "$this_command"
}

trap 'preexec_invoke_exec' DEBUG
This trick is due to Glyph Lefkowitz; thanks to [bcat] for locating the original author.

http://www.macosxhints.com/dlfiles/preexec.bash.txt

Source 2

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
demure
  • 7,337
  • 3
  • 22
  • 17
  • I understand that. However I'm trying to change the "input text" color to something _different_ than the foreground. Probably not a way to do that, but figured i'd ask :) – dylanized May 19 '13 at 15:32
0

I found that in my installation of Debian 8 (Jessie) I already have this, but it is commented by default. It is

# 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

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

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
unset color_prompt force_color_prompt

I just uncommented the line that says force_color_prompt=yes.

If you didn't already have this in your .bashrc file then you can copy this and paste it in yours.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
santiago arizti
  • 4,175
  • 3
  • 37
  • 50