I have seen many files which have this line as the first line in them. What exactly is this?
#!/usr/bin/env python
What does it mean? Why is it necessary?
It's not just /usr/bin/env python
but #!/usr/bin/env python
and this line is called a shebang.
I'm quoting Wikipedia:
Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.[8] For example, if a script is named with the path "path/to/script", and it starts with the following line:
#!/bin/sh
then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible shell), passing "path/to/script" as the first argument.
The shebang line is usually ignored by the interpreter because the "#" character is a comment marker in many scripting languages; some language interpreters that do not use the hash mark to begin comments (such as Scheme) still may ignore the shebang line in recognition of its purpose.
In my case (13.10 Desktop), /usr/bin/env python
will default to python2.7 but it could be python3.4 depending on your system defaults (e.g. 14.04 Server).
$ /usr/bin/env python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
./my_script.py
. Without the shebang, you'll have to call python to interpret the script: python ./my_script.py
May 5, 2014 at 9:50
./
. python my_script.py
works just as well. (It's specifically when a script equipped with a functioning hashbang/shebang line--or other executable--is run as a command in its own right, with its name as the first word on the command line, that ./
is useful.)
Aug 4, 2014 at 2:27
A line like this can appear on the first line of interpreted programs or scripts. It is a directive to the program loader to pass the file to the interpreter - in this case Python.
Interpreter directives are placed on the first line of an executable script after the characters #!
(called a shebang or hashbang) so that a script can be executed by just the script name, in one of the following ways from the command line:
$ script
$ ./script
$ path/to/script
To be able to execute the script in this way like a command, it must have execute permission.
instead of the interpreter with the script name as an argument which would be required if the interpreter directive was not there, like thus:
$ python path/to/script
hello.py
in the scripts directory in my home directory then the relative pathname would be scripts/hello.py
.
#!
is called shebang. Normally, Bash considers the symbol #
as a comment, however upon seeing #!
Bash knows that the rest of the content should be a script and the first line will refer to which program or interpreter to invoke. In the case of #!/usr/bin/env python
Bash knows that this line is invoking the correct "environment settings" for the Python interpreter. Thus the rest of the file's content will be run using the Python interpreter. Had the line been like #!/bin/bash
or #!/usr/bin/perl
Bash would have run the content using Bash (itself) or Perl respectively.
You can still write a Python, Bash or Perl script without mentioning this line. In that case to run your Python script you'd need to invoke from command line this way:
/usr/bin/python MyScript.py
This is because without shebang #!
and without a correct interpreter path, Bash would not know it's a script and would treat it as a text file.
execve
man page.