Skip to content
Advertisement

Can I omit the full path of a “shebang” on the first line of shell script?

I often see #!/usr/bin/env bash at the first line of shell scripts.

It seems to work fine to omit /usr/bin/ part on macos:

#!env bash

echo Yo

Can I do that on all other Unix-like platforms?

Advertisement

Answer

Using interpreter without full path is not supported everywhere. In many cases, kernel does the shebang handling and kernel normally does not know about the environment variables, including PATH. So it needs a full path in order to find the interpreter.

That’s why #!/usr/bin/env bash is used – bash is located and executed by env, so it allows bash to be anywhere in PATH, while only requiring /usr/bin/env to be in known location. You can’t use just #!bash or #!env bash, because kernel does not know, where to find them.

Using plain interpreter may work sometimes – it means that shebang handling is done by something that is aware of the environment, most likely by the active shell itself and not by the kernel.

There are good answers about the similar question also available:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement