# Pass bash variable to python

**URL:** <https://community.unix.com/t/pass-bash-variable-to-python/376860>\
**Category:** Shell Programming and Scripting\
**Created:** [December 10, 2019, 10:58am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860 "2019-12-10T10:58:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpoolfc](https://community.unix.com/letter_avatar/lpoolfc/32/6_449c533f586939b6e5f246632a46421f.png) [@lpoolfc](https://community.unix.com/u/lpoolfc)\
**Post date:** [December 10, 2019, 10:58am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860/1 "2019-12-10T10:58:59Z")

</div>

How can I pass bash Variable to python script.

```nohighlight
bash.sh
while read -r db
do
Printf "%s\n" ${db} "Found"
done < path/to/file.txt

file.txt
db1
db2
db3

python.py
print(${db}_tables.replicate.fix)

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [December 10, 2019, 11:15am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860/2 "2019-12-10T11:15:21Z")

</div>

You can simply pass the variable value as a command line argument:-

```nohighlight
python.py "${db}"

```

In you python program, you can use the `sys.argv` to accept and use argument:-

```nohighlight
import sys

if len(sys.argv) == 1:
    print "Usage: python.py <argument>"
else:
    # use sys.argv[1]

```

---

<div class="post-metadata">

**Author:** ![wisecracker](https://community.unix.com/user_avatar/community.unix.com/wisecracker/32/2800_2.png) [@wisecracker](https://community.unix.com/u/wisecracker)\
**Post date:** [December 10, 2019, 11:15am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860/3 "2019-12-10T11:15:45Z")

</div>

You could modify this method to suit your needs...

```nohighlight
# get_variable_from_external_command.py

# # with subprocess

# import subprocess
# proc = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
# out, err = proc.communicate()
# print(out)

# # without subprocess
# This method works with any Python from Version 1.4.0 to the current 3.8.0.

import os

filelist = os.popen("ls -l")
text = filelist.read()
filelist.close()
print(text)

```

EDIT:  
'Printf' in your code should read 'printf'...

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [June 23, 2020, 12:41am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860/4 "2020-06-23T00:41:02Z")

</div>

Automatically bumped on June 23, 2020, 12:41am UTC



---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [June 23, 2020, 9:32am UTC](https://community.unix.com/t/pass-bash-variable-to-python/376860/5 "2020-06-23T09:32:31Z")

</div>

```nohighlight
root@host:~# export GREETINGS='Hello'
root@host:~# python
>>> 
>>> import os
>>> os.environ['GREETINGS']
'Hello'
>>>

```
