1

I'm on OS X and am using iTerm2.

I've updated my Bash profile on several of my servers so that when I ssh into them I can tell at a glance from the tabs what boxes I'm on. When I exit from an ssh session, the shell that initiated the ssh session does not source ~/.bashrc again (which I understand to be normal operating procedure), so my tab titles and colors persist with the titles and colors they were from my ssh session.

Normally when I exit an ssh session, I will also exit the shell on the computer that I'm physically using as well. But for consistency's sake, I would like to revert my iterm2 tabs to the appropriate titles and colors for the shell they represent when I exit an ssh session.

Is there a way to source my .bashrc when I exit an ssh session?

In all of ~/.bash_profile, ~/.bashrc, and ~/.bash_logout I have the following lines:

export PROMPT_COMMAND=''
echo -e "\033];test machine name\007"

When I exit an ssh session, it looks like none of these Bash files in my home directory are sourced to reprint the tab title. When I manually source them with source ~/.bashrc the tab title reprints just fine. So, the syntax is not wrong.

2
  • Take a look at ~/.bash_logout.
    – Cyrus
    May 5 '16 at 22:10
  • @Cyrus A good idea that I didn't try. However, I have tried it and the problem persists. Please see my edit.
    – smilebomb
    May 6 '16 at 13:37
1

If you want this to be command-specific, then redefining ssh (as mentioned in another answer) is probably the best approach.

If you actually prefer something more general, bash has a PROMPT_COMMAND shell variable you can set to run a command (or multiple commands) before the shell prompt is printed, which would happen right after ssh (or any other command) returns to the shell.

2
  • Please see my edit.
    – smilebomb
    May 6 '16 at 13:28
  • 1
    You need to put the single quotes in the right place, i.e. export PROMPT_COMMAND='echo -e "\033];test machine name\007"'.
    – jjlin
    May 6 '16 at 17:53
2

Is there a way to source my .bashrc when I exit an ssh session?

Yes, define a shell function:

ssh() { command ssh "$@"; source ~/.bashrc; }
6
  • You might want to use command ssh instead of hardcoding the path. May 5 '16 at 21:28
  • @glennjackman Good suggestion. Answer updated to used command.
    – John1024
    May 5 '16 at 21:36
  • @glennjackman Can one of you point me to a resource to explain this so I can understand what's going on? Googling 'bash command' is obviously not working for me lol
    – smilebomb
    May 6 '16 at 13:39
  • command is a shell built-in. To find out what some arbitrary command is, in bash type type -a command -- that will show you all the implementations, be they built-ins, functions, aliases, or commands in your $PATH. To get the documentation for a bash builtin, type help command May 6 '16 at 13:43
  • @glennjackman Thanks. I have put the line ssh() { command ssh "$@"; source ~/.bashrc; } exactly as that as the last line in my ~/.bashrc and when I exit an ssh session the tab title persists with machine name of the ssh session.
    – smilebomb
    May 6 '16 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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