Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does /usr/bin/env work in a Linux shebang line?

I know shebang line like this:

#!/bin/sh

but I found out I can also use shebang line like this:

#!/usr/bin/env python3

This confuses me, can someone explain to me how Linux will process this one?

like image 512
user1187968 Avatar asked May 04 '17 21:05

user1187968


People also ask

What does #!/ Usr bin env node mean?

#!/usr/bin/env node is an instance of a shebang line: the very first line in an executable plain-text file on Unix-like platforms that tells the system what interpreter to pass that file to for execution, via the command line following the magic #! prefix (called shebang).

What is passing the usr bin env for in the shebang What's the difference between usr bin env python and usr bin python?

1 Answer. If you have installed many versions of Python, then #!/usr/bin/env ensures that the interpreter will use the first installed version on your environment's $PATH. If you are using Unix, an executable file that is meant to be interpreted can indicate what interpreter to use by having a #!

How does shebang work Linux?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

What is the use of #!/ Usr bin env python3?

#!/usr/bin/python3 is a shebang line. A shebang line defines where the interpreter is located. In this case, the python3 interpreter is located in /usr/bin/python3 . A shebang line could also be a bash , ruby , perl or any other scripting languages' interpreter, for example: #!/bin/bash .


2 Answers

env is the name of a Unix program. If you read the manual (man env) you can see that one way to use it is env COMMAND, where in your case, COMMAND is python3.

According to the manual, this will

Set each NAME to VALUE in the environment and run COMMAND.

Running env alone will show you what NAMEs and VALUEs are set:

$ env
TERM=xterm-256color
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
…

Therefore, /usr/bin/env python3 is an instruction to set the PATH (as well as all the other NAME+VALUE pairs), and then run python3, using the first directory in the PATH that contains the python3 executable.

like image 156
user513951 Avatar answered Oct 07 '22 22:10

user513951


Newer *nix versions will resolve this the same way as the command which works.

It looks in all directories which are set in the environmental variable $PATH, whereever it is set (global, in your .bashrc or other logon script or by hand), one by one and returns the first match.

Important is, that some linux versions create alias files (aka symlinks), e.g. debian.

Another note: the bash command alias overrides this behavior as it is performed first.

like image 30
Marcel Nolte Avatar answered Oct 07 '22 21:10

Marcel Nolte