将bash变量传递给
python脚本的最佳方式是什么?我想做一些像下面这样的事情:
$cat test.sh #!/bin/bash foo="hi" python -c 'import test; test.printfoo($foo)' $cat test.py #!/bin/python def printfoo(str): print str
当我尝试运行bash脚本时,我会收到语法错误:
File "<string>",line 1 import test; test.printfoo($foo) ^ SyntaxError: invalid Syntax
总之,这样做:
... python -c "import test; test.printfoo('$foo')" ...
更新:
如果您认为字符串可能包含单引号(‘),如下面的注释中的@Gordon所述,您可以在bash中轻松地转义这些单引号.这是另一种解决方案:
... python -c "import test; test.printfoo('"${foo//\'/\\\'}"');" ...
原文地址:https://www.jb51.cc/bash/383962.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。