Q:

Exit from an SSH session but not script

In my bash script, I do:
ssh me9@some_mad_server.com;
cd ~/apple;
echo "Before Exit"
exit
echo "After Exit"

I never see Before Exit or After Exit. I can understand why I may not see Before Exist as my script at that stage is in another console.  But I am confused if the Exit mean my script ends and hence why After Exit never gets logged.
Any help appreciated.

A:

To execute a series of commands on a remote host, you need to pass them to ssh on the command line, not execute them after the ssh call. Like this:
ssh me9@some_mad_server.com '
    cd ~/apple
    echo "Before Exit"
'
echo "After Exit"

This uses a multiline string to pass multiple commands. An exit is implicit when the end of the string is reached.
Importantly, the commands in the quoted string are executed on the remote host, while the final echo is executed on the local server. I've indented the remote commands for clarity.

