Distributing a script: Should I use /bin/gawk or /usr/bin/gawk for shebang

awkenvshebang

Is gawk in /bin or /usr/bin usually? I would go with #!/usr/bin/env gawk but then I can't use arguments. Right now I'm using #!/bin/gawk -f. The script is very long and contains a lot of single quotes and works with stdin.

The GNU Awk manual has section 1.1.4 Executable awk Programs where it uses #!/bin/awk in its example but goes on to say:

Note that on many systems awk may be found in /usr/bin instead of
in /bin. Caveat Emptor.

What do most people do? I've read sed is supposedly standardized in /bin whereas perl is supposedly standardized in /usr/bin (same page as sed link but they won't let me make a third link for this post). What about awk/gawk? Does anyone know which is more common or popular?

Best Answer

Shebang wasn't meant to be that flexible. There may be some cases where having a second parameter works, I think FreeBSD is one of them.

gawk and most utilities that come with the OS are expected to be in /usr/bin/.

In the older UNIX days, it was common to have /usr/ mounted over NFS or some less expensive media to save local disk space and cost per workstation. /bin/ was supposed to have everything needed to boot in single user mode. Since /usr/ wasn't mounted on a reliable media, /bin/ included enough utilities to make it friendly enough for general administration and troubleshooting.

This was inherited in Linux initially, but as disk space is no longer an issue and in most cases /usr/ is in the root filesystem, the current trend is to move everything in /usr/bin (at least in the Linux world). So most utilities installed by a distro are expected to be found there. Even the most basic utilities, like cp, rm, ls etc (well, not yet).

Regarding the shebang choice. Traditionally, this is something the admins or users have to edit according to their environment. For all a developer knows, in other people's systems, the interpreter could be anywhere in the filesystem (eg /usr/local/bin, /opt/gawk-4.0.1/bin). Properly packaged scripts (rpm, deb etc) come with either a dependency on a distro package (ie. the interpreter has a known location) or a config script that setups the proper hashbang during installation.