Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

enter image description here

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?

enter image description here

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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 http://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.

share|improve this answer
    
Thank you, this led me to adding trap "tput sgr0" DEBUG, which amazingly enough seems to work. –  Gerry Aug 10 '12 at 7:04

Try the reset command (available at /usr/bin/reset) or tput reset.

Adding ${RESET} at the end of the prompt seems to work.

share|improve this answer
    
Oh, you want to reset the terminal right before command execution but after typing the command. I don't know how to do that. –  lhf Aug 10 '12 at 1:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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