Hacker News new | ask | show | jobs
by derobert 5988 days ago
Much better approach (assuming all your terminals use the same colors codes, you could of course use tput if not):

  RED="\[\033[31;1m\]"
  NORMAL="\[\033[0m\]"
  PS1_INSIDE='\u@\h:\w$bad\$ '
  PS1="\`
  	retcode=\$? ;
  	if [ 0 -ne \$retcode ]; then
  		bad=\" [\\\\\\\$?=\$retcode]\"
  		echo \"$RED$PS1_INSIDE$NORMAL\"
  	else
  		bad='';
  		echo \"$PS1_INSIDE\";
  	fi;
  \`"
  unset RED NORMAL PS1_INSIDE
Examples:

  anthony@Tao:~$ true
  anthony@Tao:~$ false
  anthony@Tao:~ [$?=1]$  # actually in bold red