How to return a value from a Python script as a Bash variable?

This is a summary of my code:

# import whatever

def createFolder():
    #someCode
    var1=Gdrive.createFolder(name)
    return var1 

def main():
    #someCode
    var2=createFolder()
    return var2

if __name__ == "__main__":
    print main()

One of the ways I managed to return the value to the bash variable was to print what was returned from main (). Another way is to simply print the variable anywhere in the script.

Is there a way to get it back in a more pythonic way?

The script is called like this:

folder=$(python create_folder.py "string_as_arg")
+5
source share
4 answers

If you are working in bash, you can simply do:

export var="value"

Python . os.environ, . , .

+2

pythonic , bash python.

, bash - bash.

bash python , (IPC) . IPC, bash (, ). bash, bash, , python ( python ).

- . , .

, bash, - stdout , python print. . ( fifo), python write. .

+3

python bash script. , " " . , : Python

, , . : Python script bash script

0

sys.exit(), ..:

import sys

[...]

if __name__ == "__main__":
    sys.exit(main())
-1

All Articles