I noticed that some python scripts start with

#!/usr/bin/env python

instead of


Are python scripts the only ones that use #!/usr/bin/env? Are there any scripts that start out with for example #!/usr/bin/env bash or #!/usr/bin/env perl? If not then why are python scripts different?


Using env in the shebang of a script

Python scripts are not different from scripts in any other language on this.

Either the usage of #!/usr/bin/env python or #!/usr/bin/python plays a role if the script is executable, and called without the preceding language. The script then calls the language's interpreter to run the code inside the script, and the shebang is the "guide" to find, in your example, python.

Using #!/usr/bin/env python instead of the absolute (full path) #!/usr/bin/python makes sure python (or any other language's interpreter) is found, in case it might not be in exactly the same location across different Linux- or Unix -like distributions, as explained e.g. here.

Although #!/usr/bin/python will work on a default Ubuntu system, it is therefore good practice to use #!/usr/bin/env python instead.

About env

env is an executable in /usr/bin, or, as mentioned by @hvd (thanks for the hint!), available as a compatibility symlink in /usr/bin to env, in pretty much all Linux distributions.

From Wikipedia:

env is a shell command for Unix and Unix-like operating systems. It is used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment. Using env, variables may be added or removed, and existing variables may be changed by assigning new values to them.

and in relation to your question:

In practice, env has another common use. It is often used by shell scripts to launch the correct interpreter. In this usage, the environment is typically not changed

More on env is to be found here, and, as always, in man env (from a terminal).

Additional information on the shebang; why doesn't #!python work?

In a comment, it was asked why we cannot simply use #!python as a shebang. Since the interpreter is in $PATH, the idea is understandable.

The reason is that an executable is executed by execve, as we can read here. Specifically the lines:

An interpreter script is a text file that has execute permission enabled and whose first line is of the form:

#! interpreter [optional-arg]

The interpreter must be a valid pathname for an executable....

explain that execve demands a full (valid) path to the interpreter. That makes sense, since scripts (in any language) can e.g. be run at any moment during startup as well, possibly before $PATH is set at all.

  • 7
    For completeness, since this isn't really specific to Ubuntu, there is no formal requirement for /usr/bin/env to exist any more than there is a requirement for /usr/bin/python to exist. It could legitimately exist as /bin/env, for instance. It's just that in practice, pretty much every distro makes env available as /usr/bin/env, either directly or through a compatibility symlink. – hvd Jan 3 '16 at 14:13
  • 3
    @hvd Luckily, or we would have needed an env for env. :) – kos Jan 3 '16 at 14:58
  • 4
    Just as a tip, you should always do #!/usr/bin/env python2 or #!/usr/bin/env python3. You should always make your Python scripts use a specific version of the interpreter rather than putting just 'python'. – John Scott Jan 3 '16 at 16:39
  • 3
    Why can't we use #!python? – user423626 Apr 30 '16 at 16:56
  • 1
    @BharadwajRaju see my updated answer. – Jacob Vlijm Apr 30 '16 at 19:16

Jacob's answer explains it well. However there is one more point I would like to mention.

Using /usr/bin/env/ in python serves one more purpose. As python supports virtual environments, using /usr/bin/env python will make sure that your scripts runs inside the virtual environment, if you are inside one. Whereas, /usr/bin/python will run outside the virtual environment.

  • 1
    What is python's "virtual environment" all about? – Pacerier Aug 16 '17 at 0:17
  • 1
    "virtual environment" is a way to create application-specific custom deployments of Python, each with its own packages installed, without affecting the system-wide default installation. – YitzikC Aug 17 '17 at 9:36

In addition to the answers given above, the use of /usr/bin/env allows your Python to be installed in non-standard places, and as long as your PATH is set-up correctly, the script will run unmodified on any UNIX-style system that has /usr/bin/env. This includes any modern version of Linux and OS/X.

  • How is this any different from a symlink for /use/bin/python? – Pacerier Aug 16 '17 at 0:18
  • 1
    The user might not have permission to create such a symlink in /usr/bin, and the developer should not assume it. Also, it's possible that several Python distributions are installed side by side, and using /usr/bin/env one could choose which one to run by altering the PATH variable. – YitzikC Aug 17 '17 at 9:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.