Passing arguments to Python from Shell Script

Passing arguments to Python from Shell Script

If there is space in between argument and argument is not in quotes, then python consider as two different arguments.

Thats why the output of print data in the python code is just 1.

Check the below output.

[root@dsp-centos ~]# python dsp.py Dinesh Pundkar
In python code
Dinesh
[root@dsp-centos ~]# python dsp.py Dinesh Pundkar
In python code
Dinesh Pundkar
[root@dsp-centos ~]#

So, in your shell script, put $var1 in quotes.

Content of shell script(a.sh):

var1=Dinesh Pundkar
python dsp.py $var1

Content of python code(dsp.py):

import sys
data = sys.argv[1]
print In python code
print data

Output:

[root@dsp-centos ~]# sh a.sh
In python code
Dinesh Pundkar

Use Join and list slicing

import sys
data =  .join(sys.argv[1:])
print In python code
print data
print type(data)

Passing arguments to Python from Shell Script

Leave a Reply

Your email address will not be published. Required fields are marked *