I know how to change the color of ls output, but I would like to change the color of all terminal output. To make clear what I mean, below is a picture of my terminal. My text color is orange. However, I would want the -bash: p: command not found, test, and the output of df to be a different color, so that it is easier to see the difference between what I type. Is this possible?

enter image description here

This is how it looks with @jmhindle answer. You can change the number after setaf for a different color (e.g. 2 for green).

enter image description here


I think you can do this by installing a DEBUG trap in ~/.bash_profile.

See the answer to this superuser question.

I added the following:

preexec() { 
    tput setaf 4
preexec_invoke_exec() {
    [ -n "$COMP_LINE" ] && return
    local this_command=`HISTTIMEFORMATE= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"`;
    preexec "$this_command"

trap 'preexec_invoke_exec' DEBUG

tput setaf 4 changes foreground colour to blue.

  • Thanks! I'll update my question to include how it looks now – Niek Dec 9 '15 at 9:26
  • while :; do [ "${i:=0}" -lt 8 ] || i=0; tput setaf "$i"; sleep .5; : "$((i++))"; done & A good (and non-destructive) prank. ;) (To get rid of it—kill %1 followed by tput setaf 7.) – Wildcard Mar 18 '16 at 4:35

You must log in to answer this question.

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