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

1
  • Duplicate of 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.
    – user382051
    Commented Dec 31, 2019 at 23:19

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.