1

I wrote a script on spyder3 which calls, through function 'subprocess.call', a bash file and passes (or should do so) a variable. The problem is, not being familiar with bash coding, I don't know how to import/read this variable and (for example) print it or echo it. Here are my lines of code on spyder:

variable='test'
subprocess.call(['./path/bash_file.sh',variable])

And on the bash file:

#!/bin/sh
echo variable

But obviously something is missing, in the bash file at least(?).

0

2 Answers 2

2

either pass the variables to the bash script as arguments or environmental vars

p = subprocess.Popen(
    ("/bin/bash", "script.sh", arg1, arg2),
    ...
)
out, err = p.communicate()
custom_env = os.environ.copy()  # if you want info from wrapping env
custom_env["foo"] = arg3        # this is just a dictionary
p = subprocess.Popen(
    ("/bin/bash", "script.sh"),
    env=custom_env,
    ...
)

#!/bin/bash
echo "arg1: $1"
echo "arg2: $2"
echo "env arg: ${foo}"

note that /bin/sh and /bin/bash are different, but practically bash has some extra features which are convenient and it won't really matter here

Sign up to request clarification or add additional context in comments.

Comments

1

Bash file:

#!/bin/sh

echo "$1"

Python file:

import subprocess

path_to_bash_file = 'path_to_bash_file'

arg_1 = 10

subprocess.check_call([path_to_bash_file, str(arg_1)])

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.