[Libguestfs] shebang usage in bash scripts

Richard W.M. Jones rjones at redhat.com
Thu Aug 25 13:35:47 UTC 2022


On Thu, Aug 25, 2022 at 03:32:15PM +0200, Laszlo Ersek wrote:
> Hi,
> 
> most shell scripts in the v2v projects start with a shebang like this:
> 
> #!/bin/bash -
> 
> I *think* I understand the intent of the single hyphen, but (a) it seems unnecessary, (b) even if we insisted, using the double-hyphen separator "--" is much more idiomatic (even though the shell, per POSIX, is supposed to interpret "-" identically to "--").
> 
> Regarding why I think the hyphen is unnecessary:
> 
> - setuid shell scripts are not a thing on any platform we (should) care about
> 
> - the script name is not reinterpreted as an option *anyway*
> 
> Consider:
> 
> cat >-v <<EOT
> #!/bin/bash
> echo hello \$1
> EOT
> 
> chmod +x -- -v
> 
> PATH=$PWD:$PATH -v world
> --> hello world
> 
> That is, when we run the script under the name "-v" (with it being on the PATH) and with the single command line argument "world", we'd naively asume that the shebang translated to:
> 
>   /bin/bash -v world
> 
> where "-v" came from the script's name, and "world" would be taken as the name of a shell script to execute. Thus, the idea would be to prevent this (i.e., to pass options to the interpreter by renaming or (sym)linking the shell script).
> 
> But that's not what we actually see; what we see is consistent with the command:
> 
> /bin/bash -- -v world
> --> hello world
> 
> In fact, if I append "sleep 1000" to the script, I can also fetch:
> 
> $ hexdump -C /proc/10975/cmdline
> 00000000  62 61 73 68 00 2d 2d 00  2d 76 00 77 6f 72 6c 64  |bash.--.-v.world|
> 00000010  00                                                |.|
> 00000011
> 
> So glibc and/or the Linux kernel already inserts the "--" option/operand delimiter!
> 
> I intend to contribute a shell script to virt-p2v; do I need to use the hyphen in the shebang? (If so, I prefer the double-hyphen.)

Eric should be able to give the definitive answer here.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top


More information about the Libguestfs mailing list