0

i want to execute bash function in a script and i want to print the return function.

this is my current code

function myfunct() { 
    return 1
}

and my python code:

process = subprocess.Popen(['bash', '-c', '. myscript.sh; myfunct'])
output, error = process.communicate()
print(output)

i have "None" output, i want 1 output

1 Answer 1

2

The return value of a script isn’t streamed to a standard stream. It sets an exit code, which you can query via process.returncode.

Since you aren’t actually reading the script’s standard streams, don’t use Popen/communicate — use run instead:

process = subprocess.run(['bash', '-c', '. myscript.sh; myfunct'])
result = process.returncode
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.