Is there an escape sequence that will change the background color in Terminal?

I have this in my bash_profile:

# Change the terminal theme when SSH to remote
function tabc() {
    NAME=$1; if [ -z "$NAME" ]; then NAME="Default"; fi
    osascript -e "tell application \"Terminal\" to set current settings of front window to settings set \"$NAME\""
}

# Change the terminal color when remote
function ssh {
  tabc "Hiltmon-Remote"
  /usr/bin/ssh "$@"
  tabc "Hiltmon"
}

It uses Applescript (which you don't want) to change the terminal profile. Replace the Hiltmon and Hiltmon-Remote with your own color scheme names. When you use the ssh command, the macro changes the shell theme, then runs ssh. When you exit the remote session, it changes it back. Very cool!

I tried using the shell color escape sequences like \[\e[36m\] and \[\e[0m\] but the remote session can and does reset them. This works great for me.


Use something like

tput setab <n>

See background coloring for values to use for n. You may want to follow this by the clear command to repaint the whole window. Note that this will not persist if you embed escape codes in your prompt that reset colors or if you do something else that resets terminal colors.