in the below command, i export a value to a variable which later is used by the script, however i dont see the exported value is actually been exported.
ssh user@host "export var=/path/ ; cd /path/ ; ./script"
how can i use the above command with proper value of var remotley
You have to make your shell send a literal $ to the other end to use variables. That means putting things in single quotes, where the shell isn't allowed to substitute by itself.
One layer of quotes gets stripped out when you run the shell command, letting it substitute properly on the other side.
ssh user@host 'export var=/path/ ; cd "$var" ; ./script'
To illustrate the relative correct execution of ahamed:
ant:/home/vbe/z01 $ ssh an12 "export mypath=/etc/; cd $mypath; /home/vbe/run"
vbe@an12's password:
I am now here : /home/vbe
My path is : /etc/
ant:/home/vbe/z01 $ ssh an12 "export mypath=/etc/; cd $"mypath"; /home/vbe/run"
vbe@an12's password:
I am now here : /etc
My path is : /etc/
ant:/home/vbe/z01 $
Forgot run code...
an12:/home/vbe $ more run
#!/bin/ksh
echo "I am now here : " $(pwd)
echo "My path is : $mypath"
run: END
You are not sending a literal $ to the other end. Having put everything in double quotes instead of single quotes, $DIR gets substituted before it gets sent to the server, causing it to receive 'echo' with no arguments.