Date:        Tue, 28 Nov 2023 19:27:17 -0500
    From:        =?UTF-8?Q?Lawrence_Vel=C3=A1zquez?= <v...@larryv.me>
    Message-ID:  <b7370fe4-402e-4022-a6c8-398a7e393...@app.fastmail.com>

  | 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 [*]:


I had expected that the '[*]' footnote was going to correct things
and explain that while what you said was more or less right, it isn't
correct, which a variation on your example will show...

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

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

The -c option isn't followed by the command to execute, if
it were then ...

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

wouldn't operate like it does, as -x would then be an option.

Rather -c (nd -s) tell the shell how to interpret the args that
follow once all the options have ended, as in for example

        % bash -c -x -- -q
        + -q
        bash: line 1: -q: command not found

"--" or any arg not starting wih '-' end the options, in shells,
just like in most other commands (with the -- otherwise being ignored,
other options not).

If -c was given, then at least one non-option arg is required,
the first is the command string, the remainder, if any, are as you
explained.

If -s is given then the remaining args become $1, $2 ... and $9
is unchanged (commands are then read from stdin).

Giving both -s and -c produces results that vary from shell to
shell,, so don't do that!

Otherwise, if there are args, the first is the name of a file,
from which commands are read, and which also becomes $0, the
remainder, if any, become $1 $2 ... for the script.

Otherwise (no -s, no -c, no args) we simply have a nomaal
shell which reads stdin for commands, uses its own name as
$0 (which it also does with -s, or with -c when no replacement $0
is given) and is interactive if stdin and stderr are terminals
(and in some shells, if -i was given, but don't rely upon that).

And then:

  | From:    Klaus Frank <klaus.fr...@posteo.de>
  | Subject: Re: Missing documentation "-bash_input"

  | 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?

What command?   Consider

        bash -c 'while sleep 5; do printf %s\\n "$0"; done'

What would you expect $0 to be set to in that case?   Here, it
would just be "bash" as no new $0 was given (try it, but note
you need to wait 5 secs before the first output appears, then
5 more for each following copy of that...), or with

        bash -c 'while sleep 5; do printf %s\\n "$0"; done' foo

it will be "foo".   What else could it reasonably be?  "while" ???

Also, try adding -x, before the -c, after the -c, combined with
the -c (as -cx and -xc) and instead of "foo", and observe what
happens then as well.    Read the manual page first (all of it)
and then experiment.   If you're going to go to the code, then
read it, don't just run grep!

kre

Reply via email to