Hi,

thanks for the explanation. Esp. the parsing one at the bottom, that explains why my tests were false positive.

One thing though, I probably should already know that, but why is a $0 needed even though a command was already specified? Shouldn't the command itself be $0?



On 2023-11-29 01:27:17, Lawrence Velázquez wrote:
On Tue, Nov 28, 2023, at 5:33 PM, Klaus Frank wrote:
sorry, but this is not true
It is true.


I can clearly see that it exists. It may be
an distro addition though. Is it specific to ArchLinux? Because I can
see it being used and when I try to use it on my system it also clearly
works.
You see it being used without causing errors, but that doesn't mean
it's doing what you think it's doing.  Observe that

        % bash -c 'printf %s\\n "$@"' -bash 1 2 3

and

        % bash -c 'printf %s\\n "$@"' --no_such_option 1 2 3

appear to behave identically.


But against it just being a distro specific thing is that I also
can see it within the bash source code mirror on GitHub. Where does it
come from if it is not supposed to exist? Sorry, but something is really
confusing here.


Example usage:
https://gitlab.archlinux.org/archlinux/devtools/-/blob/master/src/makechrootpkg.in?ref_type=heads#L152

arch-nspawn "$copydir" "${bindmounts_ro[@]}" "${bindmounts_rw[@]}" \
          bash -c 'yes y | pacman -U -- "$@"' -bash "${pkgnames[@]/#//root/}"
You're misinterpreting that command line.  As Andreas already
explained, when you run something like

        bash -c 'command string here' a b c d

the first argument after the command string (in my case "a", and
in your case "-bash") is used as $0 while executing that command
string, and the remaining arguments are used as the positional
parameters.  This happens even if an argument looks like an option,
so options are not recognized as such if they follow -c [*]:

        % bash -x -c 'printf %s\\n "$0"'
        + printf '%s\n' bash
        bash
        % bash -c 'printf %s\\n "$0"' -x
        -x

In your example, bash executes the command string 'yes ... "$@"'
with "-bash" as $0 and the (possibly multiword) expansion of
"${pkgnames[@]/#//root/}" as the positional parameter(s).  I don't
know why they are setting $0 to "-bash".  Doing so makes the shell
look like a login shell at a quick glance, but that doesn't make
it one:

        % bash -c 'printf %s\\n "$0"; shopt login_shell' -bash
        -bash
        login_shell     off

---

[*]: Hypothetical "-bash" and "-bash_input" options would have to
come before -c to take effect.  However, bash does not use Tcl-style
options, so "bash -bash" would be equivalent to "bash -b -a -s -h",
and "bash -bash_input" to "bash -b -a -s -h -_ -i -n -p -u -t".
These options all exist already except for "-_", which is why Greg's
demonstration yielded the error "-_: invalid option".

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to