Maryam Azizi
Maryam Azizi

Reputation: 31

how to pass an argument from python code to bash script?

I have a python code in which at the beginning it takes a string variable let say "element_name" from user and build some sub-folders based on this string and also some output files created by this code move to those folders.

On the other hand, I have a bash script in which some codes should be running in the sub-folders made in python code.

Any help how to introduce those folders in bash? How to pass the "element_name" from python to bash?

In python code "a.py" I tried

first = subprocess.Popen(['/bin/echo', element_name], stdout=subprocess.PIPE)

second = subprocess.Popen(['bash', 'path/to/script', '--args'], stdin=first.stdout)

and then in bash

source a.py
echo $element_name 

but it doesn't work.

Upvotes: 1

Views: 89

Answers (2)

Maryam Azizi
Maryam Azizi

Reputation: 31

I found a way. What I did is to pass the argument in a bash file and import this bash file as a source to my main bash file. Now everything works well.

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

It's not clear from your question what is in your scripts, but I guess

subprocess.run(['/bin/bash', 'path/to/script', '--args', element_name])

is doing what you intend to do, passing the value of element_name to script as an argument.

Upvotes: 2

Related Questions