run command with ssh[solved]

Hi all,

Is it possible to make this possible ?

[smily@xyz ~]$ echo $SKY_HOME
/var/sink/SKY

[smily@abc ~]$ echo $SKY_HOME
/home/smily/SKY

[smily@abc ~]$ ssh root@xyz "echo $SKY_HOME"
root@xyz 's password: ******
/home/smily/SKY wrong output [for me , it's wrong ]

I was expecting the output as
/var/sink/SKY

.

---------- Post updated at 12:37 PM ---------- Previous update was at 12:04 PM ----------

Yep. I got it.

In xyz
=====
I made a shell script : /var/scripts/printEnv.sh
#!/bin/bash
source /etc/profile
echo $SKY_HOME

In abc
=====
[smily@abc ~]$ ssh root@xyz "sh /var/scripts/printEnv.sh"
root@xyz 's password: ******
/var/sink/SKY correct output [ As expected. ]

Single quotes are also a good way to delay $ expansion. In fact, using single quotes a lot and first prevents lots of surprises. In severe cases of quote and $ confusion, you can go to:

echo '. . . .' | ssh who@where bash

if you need complete and precise control of input and substitution you can do this:

ssh username@host exec /bin/sh -s "arg1" "arg2" <<"EOF"
# Nothing will substitute inside the here-document.  If you want anything
# carried inside, pass them along like the arg1, arg2, arg3 up there.
# arg1 becomes $1, arg2 becomes $2, etc.
echo "My hostname is $HOSTNAME"
EOF

I find echo '...'| does even less substitution that a <<here document. I never got a speed difference but suspect the here overhead is more than the echo. Also, the echo is directly viewable in final form. However, the $# can be handy, like having a remote script but not having a second file to maintain and distribute.