[Libguestfs] shebang usage in bash scripts

Eric Blake eblake at redhat.com
Fri Aug 26 12:50:36 UTC 2022


On Fri, Aug 26, 2022 at 01:37:37PM +0100, Richard W.M. Jones wrote:
> > 
> > Shebang interpretation is sometimes by the kernel, and sometimes by
> > the shell, and it differs by OS.  Some OS's do word-splitting on the
> > rest of the shebang after the first space, some treat the entire rest
> > of the line as a single argument.
> > 
> > For a nice summary of some of the warts of shebang lines, see
> > 
> > https://unix.stackexchange.com/questions/29608/why-is-it-better-to-use-usr-bin-env-name-instead-of-path-to-name-as-my/29620#29620
> > 
> > If you are worried about /bin/bash being available on all systems
> > where a v2v script will be installed, you probably want '#!/bin/env
> > bash' to find the first bash in PATH.  But once you add your first
> > whitespace, it is no longer portable to use any others, so while
> > '#!/bin/bash -' works, '#!/bin/env bash -' does not.  I find that the
> > issue of writing a shebang to use a program that may not always be
> > installed in the same absolute location across all OSs (and therefore
> > where '#!/bin/env name' is useful) is more common than the issue of
> > writing a shebang that wants to pass an explicit '-' or '--'
> > end-of-option marker to protect against the script being installed
> > under a name like '-c ls'.
> 
> Fedora packaging guidelines -- rightly or wrongly -- forbid #!/usr/bin/env:
> 
> https://docs.fedoraproject.org/en-US/packaging-guidelines/#_shebang_lines
> 
> although apparently RPM rewrites them automatically so perhaps we
> would not notice.

Yeah, for an executable installed as part of the system, you really DO
want it hard-coded to the interpreter also installed by the system
(because that's what was integration-tested), and not using env (which
is dependent on the user's environment on whether it behaves the same
or picks a different interpreter than what the system
integration-tested).  It's a good policy for the RPM, but less so for
scripts that are not installed as part of a single system (that is,
scripts in $HOME/bin that is network-mounted across multiple different
OSs is very much a good place to use #!/bin/env).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


More information about the Libguestfs mailing list