bash 通过 ssh 传递变量不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2416880/
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
Passing variable through ssh doesnt work
提问by lugte098
I'm trying to pass a variable through a ssh connection, like this:
我正在尝试通过 ssh 连接传递一个变量,如下所示:
working_dir="/home/user/some_dir/"
working_dir="/home/user/some_dir/"
ssh $USER@some_host 'qsub $working_dir/some_file.txt'
ssh $USER@some_host 'qsub $working_dir/some_file.txt'
The connection itself is established, but this code gives me the following error:
连接本身已建立,但此代码给了我以下错误:
working_dir: Undefined variable.
working_dir: Undefined variable.
This could be explained, by the fact that the remote machine doesn't have the variable $working_dirsince it was defined locally.
这可以通过远程机器没有变量的事实来解释,$working_dir因为它是在本地定义的。
Is there a way of getting the value in the command locally?
有没有办法在本地获取命令中的值?
回答by orithena
Try using double quotes, that should evaluate the variable locally:
尝试使用双引号,它应该在本地评估变量:
ssh $USER@some_host "qsub $working_dir/some_file.txt"
ssh $USER@some_host "qsub $working_dir/some_file.txt"
回答by Pascal MARTIN
You are using a single-quoted string -- and I suppose variables are not interpolated inside of those.
您使用的是单引号字符串——我想变量没有插入其中。
What if you try with a double-quoted string?
Like this :
如果您尝试使用双引号字符串怎么办?
像这样 :
ssh $USER@some_host "qsub $working_dir/some_file.txt"
With that, the $working_dirvariable should be interpolated on your end -- and its value sent via the ssh connection.
有了这个,$working_dir变量应该在你的一端插入——并且它的值通过 ssh 连接发送。

