將bash變量傳遞給python腳本的最佳方式是什麼?我想這樣做如下:將bash變量傳遞給腳本?
$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
如果$ foo包含任何單引號或其他字符python解釋文字內容,這將失敗有趣。 @亞當的解決方案更強大... –