Jimmy Jimmy - 6 months ago 21
Python Question

Pass bash argument to python script

I am trying to create a bash script which passes its own argument onto a python script. I want it to work like this.

If I run it as this:

script.sh latest


Then within the bash script it runs a python script with the "latest" argument like this:

python script.py latest


Likewise if the bash script is run with the argument 123 then the python script as such:

python script.py 123


Can anyone help me understand how to accomplish this please?

Answer

In this case the trick is to pass however many arguments you have, including the case where there are none, and to preserve any grouping that existed on the original command line.

So, you want these three cases to work:

script.sh                       # no args
script.sh how now               # some number
script.sh "how now" "brown cow" # args that need to stay quoted

There isn't really a natural way to do this because the shell is a macro language, so they've added some magic syntax that will just DTRT.

#!/bin/sh

python script.py "$@"