1

How can I call use a variable in bash and use the same variable in python. There are two separate file .bash and .py

.bash file

while read -r x
do
printf  "%s\n" ${x} "Found"
done < path/to/file.txt

.py file

print(${x}+"something here")

I want to establish some connection between the two file so the variable the bash holds can be used in the .py file

lfc4lyf
  • 21
  • Duplicate of https://unix.stackexchange.com/q/556555/382051 ? Have a look at this question and the links therein if you need something more advanced than my answer. –  Dec 31 '19 at 23:19

1 Answers1

2

You need to export the Bash variable otherwise it will be local to Bash:

export x

Now the variable is an environment variable and you can import it within Python like so :

import os
... os.environ['x']

As an example

import os
print(os.environ['HOME'])

returns

/home/username