Why /usr/bin/env




















Asked 5 years, 10 months ago. Active 2 years, 7 months ago. Viewed k times. Improve this question. Linux Jedi Linux Jedi 1 1 gold badge 5 5 silver badges 6 6 bronze badges. Exact stackoverflow. The shebang should read! Add a comment. Active Oldest Votes.

Using env in the shebang of a script Python scripts are not different from scripts in any other language on this. From Wikipedia : env is a shell command for Unix and Unix-like operating systems.

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!

Specifically the lines: An interpreter script is a text file that has execute permission enabled and whose first line is of the form:! Improve this answer. The disadvantage of! That means that the script could behave differently depending on who runs it.

That's probably not too likely on modern systems, but it could easily happen for a more obscure interpreter. By specifying! Another potential problem is that the!

This usually isn't an issue, but it can be. Many Perl scripts are written with! Csh scripts should use! But there could be other examples. I have a number of Perl scripts in a personal source control system that I install when I set up an account on a new system. I use an installer script that modifies the! I haven't had to use anything other than!

A minor point: the! Neither of these is likely to be a significant concern. It might be an issue if you want your script to run on a really old system, but then you're likely to need to modify it anyway.

Another possible issue, thanks to Sopalajo de Arrierez for pointing it out in comments is that cron jobs run with a restricted environment. For that particular environment and perhaps others like it , the! I haven't used virtualenv myself. Expected results: that print sys.

Each time. Although it's TRUE that! Indeed, developers don't care which interpreter is found " first ", all they care about is that their code is executed using the specified interpreter they know to be compatible with their code to ensure consistent results- wherever that may live in the filesystem Specifying the absolute path is more precise on a given system.

The downside is that it's too precise. Suppose you realize that the system installation of Perl is too old for your scripts and you want to use your own instead: then you have to edit the scripts and change!

Attempting to operate with a bad PATH is very rarely useful, and indicates that you know very little about the system the script is running on, so you can't rely on any absolute path anyway.

For example, a bash script that only works on Linux can safely use! A script that is only meant to be used in-house can rely on house interpreter location conventions. It's more flexible than specifying an absolute path but still requires knowing the interpreter name. In such cases, you can often make a polyglot script that can be interpreted both by the standard shell and by your desired interpreter. For example, to make a Python 2 script portable both to systems where python is Python 3 and python2 is Python 2, and to systems where python is Python 2 and python2 doesn't exist:.

First, it's not portable. Second, as Keith Thompson has noted, it can cause trouble with passing arguments on the shebang line. The maximally portable solution is this:. This is because the! It's part of the kernel's binary loader. This can be tested. Put this in a file and mark it executable:. If a file is marked executable and begins with a! It doesn't solve the problem of specifying the full path to the interpreter, it just moves it to env.

This prevents your script's name from appearing in, e. Changing your PATH is more work than just editing the first line of a script, especially when scripting such edits is trivial. Changing the order of PATH directories is significantly more difficult If you need to do that, it's much easier to just have several! One downside is that you will be unable to pass more than one argument e. Another downside is that since you aren't calling an explicit executable, it's got the potential for mistakes, and on multiuser systems security problems if someone managed to get their executable called bash in your path, for example.

In some situations, the first may be preferred like running python scripts with multiple versions of python, without having to rework the executable line.

But in situations where security is the focus, the latter would be preferred, as it limits code injection possibilities. It can be useful if you aren't aware of the absolute path or don't want to search for it. If the shell scripts start with! If they however start with!

For scripts that are only expected to be run in an activated virtual environment, shebang lines can be written as! The shebang! Since python may be an alias for python2 or python3 typically, it is an alias for python2 , the developer may choose to be specific and use the shebang! For Windows users, it is easy to conflate env with Python virtual environments as opposed to the env tool in Linux.



0コメント

  • 1000 / 1000