I want to distinguish my command from the rest of the output of the shell easily through different colors. But I don't have much experience with customizing my bash shell, so I don't know how to reset the color (after) my input.

my current PS1 variable looks like this:

export PS1="$red\u$green\$(__git_ps1) $turk\w
$white$ "

# '\$(__git_ps1)' git status prompt (generates a space before it even if empty)

So my input is white. But even the output of the commands is white because it is not reset. Furthermore, if the command itself color codes its output, then it itself resets the colors which result in some ugly mixing of white and gray.

So how do I reset the color after my input command?

2 Answers 2


The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)

With this:

$ promptcol="$(tput sgr0)$(tput setaf 3)"
$ cmdcol="$(tput sgr0)$(tput bold)"
$ normalcol="$(tput sgr0)"
$ trap 'echo -n "$normalcol"' DEBUG
$ PS1="\[$promptcol\]\w\$ \[$cmdcol\]"

I get this:

colored prompt

Note that you need the \[...\] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the \[...\] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.

  • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "\e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
    – Lockon2000
    Dec 26, 2017 at 17:13
  • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red\]\u\[$green\]\$(__git_ps1) \[$turk\]\w \[$white\]$ "
    – Lockon2000
    Dec 26, 2017 at 17:16

On the ~/.bash_logout Insert the following:

echo -e "\033[0m"
  • 1
    maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
    – Lockon2000
    Dec 26, 2017 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .