Child pages
  • Find absolute path shell script
Skip to end of metadata
Go to start of metadata

How to run the shell from a shell script without knowledge about where the interpreter is installed

The Unix env Lookup Trick

On some Unix systems, you can avoid hardcoding the path to the interpreter in your script file by writing the special first-line comment like this:

#!/usr/bin/env bash

When coded this way, the env program locates the interpreter according to your system search path settings (in most Unix shells, by looking in all the directories listed in your PATH environment variable). This scheme can be more portable, as you don’t need to hardcode an install path in the first line of all your scripts.

Provided you have access to env everywhere, your scripts will run no matter where bash lives on your system. Of course, this assumes that env lives in the same place everywhere (on some machines, it may be in /sbin, /bin, or elsewhere);