6

This question already has an answer here:

I need to capture screenshots of the terminal where the typed commands visually "stand out" from the rest. Right now I'm using this to make my typed commands "red":

PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;34m\]\$ \[\e[m\]\[\e[0;31m\]'

The problem is: everything after the prompt is red, not only my typed text

red linux output

How can I fix this?

I'm using bash (CentOS 7).

marked as duplicate by Michael Homer, jasonwryan, Archemar, cuonglm, Anthon Apr 27 '15 at 9:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

You can use a trap to achieve this: trap 'echo -ne "\e[0m"' DEBUG

According to bash's man: a trap on DEBUG executes

before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So every time you execute the command, the shell will insert \e[0m disabling the previous color code. Only to have it re-set via the prompt, once the command has executed.

I found the admitedly hackish trick here: https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Different_colors_for_text_entry_and_console_output

-1
PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;34m\]\$ \[\e[m\]\[\e[1;31m\]'

You can fix this by changing the output colour, below is an example for the same.

PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;34m\]\$ \[\e[m\]\[\e[1;32m\]'

The above command displays the below output in green

  • 1
    No, this only changes the colors of the prompt but leaves the command line and the output from programs in the same color. – Gilles Apr 26 '15 at 18:25
  • PS1 is the prompt, what the question is asking is the input text. It's completely different. – believesInSanta Dec 29 '18 at 11:32

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