navigator
navigator

Reputation: 1596

Passing arguments to python interpreter from bash script

Sorry this is a very newbie question, but I just can't seem to get it to work.

in my bash script, I have

python=/path/to/python
script=$1
exec $python $script "$@"

How would I pass an argument, say -O to the python interpreter? I have tried:

exec $python -O $script "$@"

And have tried changing python variable to "/path/to/python -O", as well as passing -O to the script, but every time i do any of these three, I get import errors for modules that succeed when I remove the -O.

So my question is how to tell the python interpreter to run with -O argument from a bash script?

Thanks.

Upvotes: 0

Views: 1328

Answers (1)

konsolebox
konsolebox

Reputation: 75488

You should shift your positional parameters to the left by 1 to exclude your script which is in the first arguments from being included to the arguments for python.

#!/bin/sh
python=/path/to/python
script=$1; shift
exec "$python" -O "$script" "$@"

Then run the script as bash script.sh your_python_script arg1 arg2 ... or sh script.sh your_python_script arg1 arg2 ....

Upvotes: 1

Related Questions