5

执行此命令时:

user@local:~ >ssh user@remote " export myvar=myvalue ; echo myvar=$myvar ; "

我得到输出:

myvar=

直接在远程机器上运行时,我得到了预期的结果:

user@remote:~ > export myvar=myvalue ; echo myvar=$myvar ;

输出:

myvar=myvalue

那么如何在 ssh 命令中设置变量呢?

4

1 回答 1

13

That's because you're using double quotes, so the variable is being expanded on your local machine before ssh is even invoked. Use single quotes:

ssh user@remote 'export myvar=myvalue ; echo myvar=$myvar'
于 2013-09-27T14:05:14.820 回答