Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass a full String variable from shell script to python in one sys.argv

I would like to pass a String variable from Shell Script to my python script and have it stored in sys.argv[1].
Currently this is my situation:

main.sh

TEST="This is a test"
python main.py $TEST

main.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if __name__ == '__main__':
     print(sys.argv[1])

result:

This

How do I send $TEST so that sys.argv[1] = "This is a test"?
I don’t want to have to reconstruct the String after sending it.

>Solution :

Edit your main.sh to following :

TEST="This is a test"
python main.py "${TEST}"

Variables need to expanded inside "" and will be transformed into its
value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading