如何将字符串变量从shell脚本传递到sys.argv的python?
我想将字符串变量从shell脚本传递给我的python脚本,并将其存储在sys.argv [1]
中。 目前,这是我的情况:
main.sh
TEST="This is a test"
python main.py $TEST
main.py
if __name__ == '__main__':
print(sys.argv[1])
结果:
This
如何发送$ test
,以便sys.argv [1] =“这是一个测试”
? 我不想在发送字符串后重建该字符串。
I would like to pass a string variable from a Shell Script to my python script and have it stored in sys.argv[1]
.
Currently, this is my situation:
main.sh
TEST="This is a test"
python main.py $TEST
main.py
if __name__ == '__main__':
print(sys.argv[1])
result:
This
How do I send $TEST
so that sys.argv[1] = "This is a test"
?
I don't want to have to reconstruct the String after sending it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑您的 main.sh 以下:
变量需要扩展内部“”,并将被转换为其
价值。
main.py
结果:
Edit your main.sh to following :
Variable needs to expanded inside "" and will be transformed into its
value.
main.py
Result:
只是使用:
Just use: