[Tutor] Confusion with Python, Bash and Command Prompt

Modulok modulok at gmail.com
Fri Aug 10 07:35:08 CEST 2012


...
> My Question:
> Is it true that doing that is as same as doing #!/usr/bin/env python
> on Unix? Because I think that the matter of shebang is limited to Bash
> and Windows don't have a bash, it has a Command Prompt. And I don't
> think such thing happens in Windows.

It has nothing directly do with bash. It has to do with the operating system's
'program loader'. It's the low level code responsible for setting up an
execution environment for a new process. It looks for environment variables,
etc. For example on FreeBSD most shebang parsing defined in envopts.c.

Basically, whenever you request a file to be executed, the program loader (not
your shell) looks at the first line of the file for a shebang. If found, it
executes the file the shebang line references and passes the path to the
current script file as the first argument.

That said, different operating systems' program loaders', regardless of the
shell you're using (be it bash, tcsh, sh, etc), will process shebang lines
differently. Some ignore them entirely. The syntax can even vary slightly from
one OS to another (and not just the file paths).

So does it apply to Windows? No. Windows has no concept of shebang lines. File
types are determined exclusively their extensions, as is the executability of a
file. (There's a registry looking that occurs to determine what program to pass
the file to.)

If you rename a python script from foo.py to foo.jpg, windows will attempt to
open it in an image viewer, regardless of any shebang lines present. (Contrast
this with most unix-like flavors where extensions are meaningless.
Executability is determined by a file-system level permission bit and the
interpreter is determined by the program loader reading shebang lines. The file
would be passed to the python interpretter, assuming a correct shebang.)

You are right in your assumption; It does not apply to Windows. The tutorial is
incorrect.

-Modulok-


More information about the Tutor mailing list