1

I have a bash script which ssh's to a server, and depending on the status of a variable performs a task:

#!/bin/bash

foo=$1
ssh [email protected] '
echo In host
if [ "$foo" == "yes" ]; then
   echo "Foo!"
fi
'

When I run sh script.sh yes, although the ssh command works, the conditional evaluates to false. I can see this if I echo $foo - it prints an empty line. How can I access the value of foo within the ssh command?

1 Answer 1

1

Variables aren't transferred to a remote machine. You can expand the variable in the code sent through ssh, but you have to be extremely careful because it opens the door to uncontrolled code execution:

#!/bin/bash

foo=$1
ssh [email protected] '
echo In host
if [ "'"$foo"'" == "yes" ]; then
   echo "Foo!"
fi
'

Now imagine (don't try) what happens if foo='$(rm -rf /)'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.