The shebang line (from “sharp bang”, i.e. #!) is processed by the kernel. The kernel doesn’t want to know about environment variables such as PATH. So the name on the shebang line must be an absolute path to an executable. You can also specify an additional argument to pass to that executable before the script name (with system-dependent restrictions I won’t go into here). For example, for a Python script, you can specify


on the first line, and when you execute the script, the kernel will in fact execute /usr/bin/python /path/to/script. But that’s not convenient: you need to specify the full path of the command. What if you have python in /usr/bin on some machines and /usr/local/bin on others? Or you want to set your PATH to /home/joe/opt/python-2.5/bin so as to use a specific version of Python? Since the kernel won’t do the PATH lookup for you, the idea is to make the kernel run a command that in turns looks up the desired interpreter in the PATH:

#!/fixed/path/to/path-lookup-command python

That path-lookup-command must take the name of an executable as an argument and look it up in PATH and execute it: the kernel will run /fixed/path/to/path-lookup-command python /path/to/script. As it happens, the env command does just that. Its main purpose is to run a command with a different environment, but since it looks up the command name in $PATH, it’s perfect for our purpose here.

Although this is not officially guaranteed, historic Unix systems provided env in /usr/bin, and modern systems have kept that location precisely because of the widespread use of #!/usr/bin/env. So, in practice, the way to specify that a script must be executed by the user’s favorite Python interpreter is

#!/usr/bin/env python

This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *