19

What is the purpose of using #!/usr/bin/env <sh|bash|python> shebang compared to #!/bin/bash, #!/bin/sh or #!/usr/bin/python?

Flimm
  • 40,306
  • 22
  • 94
  • 154
Tuminoid
  • 3,942
  • 1
  • 21
  • 28
  • 4
    There is a good answer [here](http://askubuntu.com/questions/88257/what-type-of-path-in-bash-is-more-preferable/88314#88314) in regards to the general use of env. – Kevin Bowen Jan 24 '13 at 09:37

2 Answers2

11

Some people might use a different python (perl, etc.) than the system one. /usr/bin/env python would run the version configured as the current one, possibly making the script more portable.

On the other hand, reportedly, some systems do not have /usr/bin/env. Also, you cannot use #!/usr/bin/env foo x as a replacement for #!foo x, because foo x will be interpreted as a single argument. So the value of the approach is debatable.

Andrea Corbellini
  • 15,616
  • 2
  • 64
  • 81
choroba
  • 9,273
  • 1
  • 30
  • 42
  • 1
    Configured where? From `info coreutils 'env invocation'` I can see this form of calling makes sure it is external binary instead of shell built-in, but no indication how `env python` would be any different from `python`? – Tuminoid Jan 24 '13 at 09:15
  • 4
    Your `PATH` probably includes `/usr/local/bin` first. If somebody has manually installed python there, then `env` will call it, whereas `#!/usr/bin/python` will bypass it. – Robie Basak Jan 24 '13 at 09:44
  • 2
    `virtualenv` is a quite popular tool, and it installs a python executable in the working directory. You would need the `env` trick to get it to work correctly. – Flimm Jan 29 '13 at 21:38
  • 2
    @Tuminoid: you are right: `env python` is the same as `python`. However the problem is that you can't write a shebang like `#!python`: you need to specify a full path. – Andrea Corbellini Jan 29 '13 at 22:36
0
#!/usr/bin/env python
#!/usr/bin/python

The first one is more portable. source1. source2. source3. I haven't seen Linux a system where it doesn't work. And this is a general statement - /usr/bin/env is often available, other binaries sit around the filesystem wherever they please.

Vorac
  • 319
  • 4
  • 17