6

Is there a way I can have multiple shebangs?

So I can call #!/usr/bin/env python3 on Ubuntu, but MacOS doesn't seem to have an equivalent, and I would like to call #!/usr/bin/python3 instead on it.

So is it possible to do something like:

#!/usr/bin/env python3
#!/usr/bin/python3
codeforester
  • 39,467
  • 16
  • 112
  • 140
A. L
  • 11,695
  • 23
  • 85
  • 163

1 Answers1

12

We can't have multiple shebang lines - there can only be one and it should always be the first line.

If you need to support multiple versions of Python based on OS, it is best to write a small shell wrapper that invokes your python script with the right interpreter, probably with an exec.

macOS does have /usr/bin/env.


See this post:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • I'm making an application that's got a php backend but uses some python scripts. For some reason, it doesn't work on one system, but works on the other with `/usr/bin/env`. The ubuntu is on AWS – A. L Mar 23 '17 at 03:29
  • 1
    There is an exception to the one shbang line rule which if the program that the first shbang line execs can itself understand what to do with multiple shbang lines, then that works. nix-shell can do this and people use it to dynamically load interpreters and dependencies in the environment for scripts. – TKH May 05 '21 at 03:31