从bash运行python时,输入将丢失字符串格式

从bash运行python时,输入将丢失字符串格式,python,string,bash,ubuntu,ssh,Python,String,Bash,Ubuntu,Ssh,我通过ssh登录并运行bash。在这些bash命令中,我需要调用python文件中的特定函数,并将字符串参数传递给该函数 我现在所拥有的: echo "commands" | ssh into instance 其中命令包含 python -c 'from python file import myfunction; myfunction({}) ' ... .format(myvariable) myvariable是一个包含多个单词的字符串 但是,当ssh完成并执

我通过ssh登录并运行bash。在这些bash命令中,我需要调用python文件中的特定函数,并将字符串参数传递给该函数

我现在所拥有的:

echo "commands" | ssh into instance
其中命令包含

python -c 'from python file import myfunction;
            myfunction({}) ' ... .format(myvariable)
myvariable是一个包含多个单词的字符串

但是,当ssh完成并执行命令时,调试日志显示myvariable周围的引号消失了,当传递到myfunction时,我会收到一个未定义输入变量的错误


如何确保myvariable保持为字符串?

这通常是由于认为引号是上下文感知的。他们不是

例如:

var="select * from table where name="value" order by name"
作为一个人类,你可能会认为,由于这是一个SQL语句,显然BASH会知道中间的引号是嵌套的,并且将这样读取字符串:

var="select * from table where name="value" order by name"
                                    |-----| Nested
    |----------------------------------------------------| Everything
var="select * from table where name="value" order by name"
    |-------------------------------|     |--------------|
           Quoted string #1                   Quoted #2
然而,Bash并没有那么聪明。它读取的字符串如下所示:

var="select * from table where name="value" order by name"
                                    |-----| Nested
    |----------------------------------------------------| Everything
var="select * from table where name="value" order by name"
    |-------------------------------|     |--------------|
           Quoted string #1                   Quoted #2
文字值是串联的,其净效果是中间的引号“消失”。该声明相当于:

var="select * from table where name=value order by name"
相反,您必须告诉bash引号是带有转义的文本数据:

var="select * from table where name=\"value\" order by name"

自动对此发出警告

这不是对您的问题的回答,但您可能希望查看而不是打印命令