10

I want to execute a python script via “Here Document” following some arguments in my bash shell script, as follows

python <<'__SCRIPT__'
...
__SCRIPT__
ARG1 ARG2 ...

But don't know how to give these arguments. I have tried putting them following python, following SCRIPT and a new line right after SCRIPT. But errors are reported in all of the cases when executed.

So what is the right way?

BR, RUOCHEN

devnull
  • 118,548
  • 33
  • 236
  • 227
Pan Ruochen
  • 1,990
  • 6
  • 23
  • 34

2 Answers2

9

The arguments are part of the command.

python - arg1 arg2 << ...
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
5

<<__SCRIPT__ is not actually a script passed to python, it's a stream containing the script. You have to tell python where to get the script, which is - in this case. That's why python - arg1 arg2 <<'__SCRIPT__':

$ cat here-py.sh
python - foo bar <<__SCRIPT__
import sys
print(sys.argv)
__SCRIPT__
$ ./here-py.sh
['-', 'foo', 'bar']
aragaer
  • 17,238
  • 6
  • 47
  • 49