Execute a command after ssh exits

In my .tcshrc I define and run an alias to set my terminal title. The title contains the hostname. I do this because I ssh to other machines a lot and I always need to know which host I am typing in. The alias is defined and run in the .tcshrc like this...

alias tt 'echo -n "^[]0;`hostname -s`^G"'
tt
#

(where "^[" and "^G" are special control characters)

This is great when I ssh into a machine. The problem is when I exit the ssh , because the title will still show the now-old hostname. I have to manually run tt again after I exit ssh .

Is there any way this can be done automatically?

I don't use tcsh (or other csh derivatives), but with the Bourne shell and its derivatives you can set PS1 (the prompt string that is written to the terminal as a prompt to enter the next command when the last executed command completes. You could run tt in a command substitution when the prompt string is printed or (as many people do) you could print the hostname and current working directory as part of the command prompt. I assume csh and its derivatives have a similar method of setting the command prompt that is written when the shell is ready to read a command line when you are running the shell interactively.

The $PS1 in sh corresponds to $prompt in tcsh.
For example, set your shell prompt to

set prompt="$USER@`hostname -s`:^[]0;`hostname -s`^G "

Then your terminal title is updated with every shell prompt.

Thanks Don and Germany. This is the right train of thought.

To get it to work I had to escape the string that sets the terminal title. Like this...

set prompt="$USER@`hostname -s`:%{^[]0;`hostname -s`^G%} "

*Note: This escaped string cannot be at the end of the $prompt string. You'll notice a space character after the escaped string in the example above. If you don't want that last space character, put the escaped string in front...

set prompt="%{^[]0;`hostname -s`^G%}$USER@`hostname -s`:"

Thanks for your help, guys. Problem solved.

Did you consider that for neither the hostname nor the username you need to run an external program? man tcsh :

Good point.

set prompt="%{^[]0;%m^G%}%n@%m:"

is more efficient (and portable to non-Linux systems).