Bash:在 SSH 命令中使用 EXPORT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19052607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: using EXPORT inside SSH command
提问by przno
When executing this command:
执行此命令时:
user@local:~ >ssh user@remote " export myvar=myvalue ; echo myvar=$myvar ; "
I get output:
我得到输出:
myvar=
When running directly on remote machine, I get the expected result:
当直接在远程机器上运行时,我得到了预期的结果:
user@remote:~ > export myvar=myvalue ; echo myvar=$myvar ;
Output:
输出:
myvar=myvalue
So how to set a variable inside ssh command?
那么如何在 ssh 命令中设置一个变量呢?
回答by glenn Hymanman
That's because you're using double quotes, so the variable is being expanded on your localmachine beforessh is even invoked. Use single quotes:
那是因为您使用了双引号,因此在调用 ssh之前,该变量已在本地计算机上展开。使用单引号:
ssh user@remote 'export myvar=myvalue ; echo myvar=$myvar'