All of the approaches that I have seen for changing the background color in Terminal use applescript; is there an escape sequence? All of the hints that I've seen, like this one, use AppleScript. I want to put it in my .base_profile and in the 'ssh' command so that when I hop from system to system the color changes properly. (I sometimes have to hop multiple systems in due to connectivity issues)

2 Answers 2


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.

  • This doesn't work for me because I am using SSH from A->B and then from B->C. The shell running on C needs a way to change the color of the Terminal running on A.
    – vy32
    Commented Sep 14, 2012 at 21:19
  • Create a ssh2 command in the profile to set a different terminal theme when you ssh from a->b->c. So you go ssh2 b and then ssh c. Shells on remote machines cannot change the theme on the local machine as far as I know.
    – Hiltmon
    Commented Sep 15, 2012 at 2:13
  • That's a bit awkward. I recently found that there is an escape sequence that will change the background of an xterm but not GNOME Terminal. See superuser.com/questions/592090/…. SO there is probably a way to do it with Terminal.app. If only I had a list of all the escape sequences that Terminal supports.
    – vy32
    Commented May 9, 2013 at 12:32

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.

You must log in to answer this question.

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