Shell script: ssh to remote machine, then exit: ‘exit’ does not work

bashcommand lineexitshellssh

I have a shell script of the following type:


echo "Hi"

I run it locally to do something on a remote server (represented by 'echo
"Hi"'). However, when I run it, I see the prompt on the remote server — so the
'exit' command is not executed. When I then manually type 'exit' on the remote
prompt, I then see "Connection to" closed and then
"Hi". How can I set up the shell script such that it exits correctly and shows
me the (local) prompt from which I executed it? and ssh and shell through ssh : how to exit? seem somewhat related, but I couldn't adapt the ideas presented there./


The following, not so minimal version leads to Unmatched '..


date=`date "+%Y-%m-%d"`
rsync -acxzP --delete --link-dest=/u3/mylogin/backup/old_backup /home/mylogin$date\_backup
ssh bash -c "'
rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup

If I remove the double quotes in this snippet, I get: bash: -c: option requires an argument and date: Undefined variable.

Best Answer

This should solve your problem.

ssh << HERE
rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup

Or you can do the following. Put all the commands you want to run on the remote host in a separate script. Give it a name like

Now run the following

ssh 'bash -s' < /path/to/

Where contains.

rm -f /u3/mylogin/backup/old_backup
ln -s $date\_backup /u3/mylogin/backup/old_backup