How to reset the terminal before command execution?
I've been looking at spicing up my Terminal experience by among other things, adding some color. I used the tput
command to build a nicely looking prompt. This is part of my .bash_profile
:
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BOLD=$(tput bold)
REVERSE=$(tput rev)
RESET=$(tput sgr0)
export PS1="${WHITE}$(date "+%H:%M") ${MAGENTA}\u${WHITE}@${MAGENTA}$(scutil --get ComputerName)${WHITE}:${MAGENTA}\w\n${GREEN}\$ ${REVERSE}"
Notice the ${REVERSE} (or $(tput rev)
) at the end of the prompt, which makes the commands I type stand out by adding a background color.
My problem however is, that when executing any command, the style is not automatically reset upon pressing enter, which in this case adds an undesired green background to the command's output as well. How can I reset the terminal right before command execution?
Solution 1:
What you're looking for in BASH is equivalant to what is built-in precmd
in ZSH. This built-in function would be executed prior to executing a command. A work around is mentioned in https://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command. I hope it gives you a hint of how to solve your problem. The reset has to be called within this function.
Solution 2:
Try the reset
command (available at /usr/bin/reset
) or tput reset
.
Adding ${RESET}
at the end of the prompt seems to work.