Hacker News new | ask | show | jobs
by adrianmonk 870 days ago
> The use of env is considered a good practice when writing shell scripts, used to tell the OS which shell interpreter to use to run the script

When using a shebang line, the reason for 'env' is actually something different.

You can just leave out 'env' and do a shebang with 'bash' directly like this:

    #! /usr/bin/bash
But the problem with that is portability. On different systems, the correct path may be /bin/bash or /usr/bin/bash. Or more unusual places like /usr/local/bin/bash. On old Solaris systems that came with ksh, bash might be somewhere under /opt with all the other optional software.

But 'env' is at /usr/bin/env on most systems, and it will search $PATH to find bash for you, wherever it is.

If you're defining a Docker container, presumably you know exactly where bash is going to be, so you can just put that path on the shebang line.

TLDR: You don't have to have a shebang, but you can have a shebang at no cost because your shebang doesn't need an env.