Silent_Rebel Silent_Rebel - 1 year ago 96
Bash Question

Assigning variables inside remote shell script execution over SSH

I am trying to execute some shell script on a remote server via SSH.

Given below is a code sample:

ssh -i $KEYFILE_PATH ubuntu@$TARGET_INSTANCE_IP "bash" << EOF
cat /home/ubuntu/temp.txt
string=$(cat /home/ubuntu/temp.txt )
echo $string

cat prints the expected result but
prints nothing.

How do I store the return value of
in a variable?

Answer Source

You need to make the content of Here doc literal otherwise they will be expanded in the current shell, not in the desired remote shell.

Quote EOF:

ssh .... <<'EOF'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download