variables inside an ssh session

Hello all,

I would like to declare and use variables inside an ssh session. I have the feeling that it's not possible. Here is the situtation simpified:

#:/bin/sh
 
 test="salut"
 
 echo $test
 
 ssh hudson@10.41.21.99 <<EOF
 
 export testssh="salut"
 
 echo testssh=$testssh
 
 EOF

and this is my output:

salut
hudson@10.41.21.99's password:
testssh=

any ideas please?

---------- Post updated at 01:44 PM ---------- Previous update was at 01:19 PM ----------

I just found the solution:

we have to put single quotes around the EOF:

 ssh hudson@10.41.21.99 <<'EOF'
 
 export testssh="salut"
 
 echo testssh=$testssh
 
 EOF 

..... !!! :slight_smile:

---------- Post updated at 03:19 PM ---------- Previous update was at 01:44 PM ----------

now I have a new problem: the variables declared in the parent bash are no longer visible inside an ssh session...

so when I execute:

#:/bin/sh

test="salut"

echo $test

ssh hudson@10.41.21.99 <<'EOF'

testssh="salutssh"

echo testssh=$testssh
echo test=$test

EOF

I have

salut
hudson@10.41.21.99's password:
testssh=salutssh
test=

any help please!? what are finally the rules for using variables in an ssh session?

I suppose it explains why some people use the expect utility...
You could try to use File Descriptors, search this forum, Perderabo has posted some goodies on the subject some time ago...

Hi.

A quote from the (ksh) man page:

 <<[-]word
   ...The resulting document, called  a  here-
    document,  becomes  the standard input.  If any character
   of word is quoted, then no interpretation is placed  upon
   the  characters  of  the  document;  otherwise, parameter
   expansion, command substitution, and arithmetic substitu-
   tion  occur,  \new-line is ignored, and \ must be used to
   quote the characters \, $, `.

With this:

#:/bin/sh
test="salut"
echo $test
ssh hudson@10.41.21.99 <<EOF 
export testssh="salut"
echo testssh=$testssh
EOF

the variable testssh is evaluated before it get's to the remote server.

See the output of:

#:/bin/sh
test="salut"
export testssh="salut_loacl"
ssh hudson@10.41.21.99 <<EOF 
echo testssh=$testssh
EOF 

You can escape the variables you want evaluated on the target server:

#:/bin/sh

test="salut"

echo $test

ssh hudson@10.41.21.99 <<EOF

testssh="salutssh"

echo testssh=\$testssh
echo test=$test

EOF

Thanks scottn, Ive been fooled by the code message:

Hello scottn,

yes indeed, it works.

thanks a lot.