set bash variable from python script

前端 未结 2 1152
礼貌的吻别
礼貌的吻别 2021-01-23 12:32

i\'m calling a python script inside my bash script and I was wondering if there is a simple way to set my bash variables within my python script.

Example:

My bas

2条回答
  •  长发绾君心
    2021-01-23 12:55

    shlex.quote() in Python 3, or pipes.quote() in Python 2, can be used to generate code which can be evaled by the calling shell. Thus, if the following script:

    #!/usr/bin/env python3
    import sys, shlex
    print('export foobar=%s' % (shlex.quote(sys.argv[1].upper())))
    

    ...is named setFoobar and invoked as:

    eval "$(setFoobar argOne)"
    

    ...then the calling shell will have an environment variable set with the name foobar and the value argOne.

提交回复
热议问题