Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 218
Jimmy Avatar asked Jan 15 '13 15:01

Jimmy


1 Answers

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 "$@"
like image 106
DigitalRoss Avatar answered Sep 29 '22 21:09

DigitalRoss