How to exit from ssh session created from bash script?

16,458

You may want to code a here document

#!/bin/bash
ssh -o "StrictHostKeyChecking no" user@$1 << ENDHERE
   here do some remote stuff
   also here
   exit
ENDHERE

If you code <<-ENDHERE the initial spaces or tabs in each line of the here document are suppressed. Parameters (like $foo) are substituted unless you code <<\ENDHERE, and the ENDHERE can actually be any word, conventionally in capitals.

Read the Advanced Bash Scripting guide (which does have some imperfections)

Share:
16,458
user784637
Author by

user784637

Updated on June 20, 2022

Comments

  • user784637
    user784637 almost 2 years

    I have the following bash script test.sh

    #!/bin/bash
    ssh -o "StrictHostKeyChecking no" user@$1
    #do some stuff
    exit
    

    However if I run this script ./test.sh I need to enter ctrl+d to get out of the ssh session and it appears as if the exit command is not working the way I intended.

    Is there any way I can exit this ssh session that was created from this bash script?