Skip to main content
edited tags
Link
Hennes
  • 65.3k
  • 7
  • 113
  • 168
Source Link
JK Laiho
  • 203
  • 3
  • 8

Passing parameters to python -c inside a bash function?

I need to print the value of a Django setting while in a Bash shell.

Basically, the command I'd like to run is python -c 'from django.conf import settings; print settings.MEDIA_ROOT'. I need to be in a certain path and pass DJANGO_SETTINGS_MODULE to it though, so I thought I'd wrap the whole thing inside a bash function that I could call instead, pyt -c [...]

I've used $@ successfully in the past in similar situations, so I was hoping it would help here, too. But it's not working as I've got it set up right now.

function pyt {
    cd $PROJECT_ROOT # set elsewhere, parent dir of my_site
    DJANGO_SETTINGS_MODULE=my_site.settings python $@
    cd $OLDPWD
}

If I type pyt -c "print 'foo'" to test it, "foo" is not printed. A blank line is. python -c "print 'foo'" prints it correctly, so it's something to do with my function here.

If I put echo $@ inside the function, all the parameters are printed out just fine. The Python interpreter is not started either (it is if I just type pyt), so the -c parameter seems to be having some effect—nothing just gets printed out.

What am I missing?