jcalfee314
jcalfee314

Reputation: 4840

piping a script to ssh, unable to set variable

I expected it to print v=1. Why does this print v=?

cat<<DONE|ssh user@host
v=1
echo v=$v
DONE

On host, bash is the shell.

Upvotes: 2

Views: 716

Answers (2)

michas
michas

Reputation: 26555

Your code is equivalent to:

echo "v=1;echo v=$v"|ssh user@host

What you want is:

echo 'v=1;echo v=$v'|ssh user@host

You can achieve this by using cat<<'DONE' instead of cat<<DONE.

Upvotes: 7

Dark Falcon
Dark Falcon

Reputation: 44201

Variables are expanded inside heredocs. You need to escape the $ with a \

Upvotes: 4

Related Questions