user784637
user784637

Reputation: 16092

How to exit from ssh session created from bash script?

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?

Upvotes: 6

Views: 17129

Answers (1)

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)

Upvotes: 16

Related Questions