2

There are two bash usages:

$ bash -c 'echo $0'
bash

$bash -c 'echo $0' "text"
text

Why in the first case parameter $0 holds the program name but in second the first parameter?

scdmb
  • 123

1 Answers1

9

Per the bash man page:

   -c        If the -c option is present, then commands are read from  the
             first non-option argument command_string.  If there are argu‐
             ments after the command_string,  they  are  assigned  to  the
             positional parameters, starting with $0.

And further down:

   If arguments remain after option processing, and neither the -c nor the
   -s option has been supplied, the first argument is assumed  to  be  the
   name  of  a file containing shell commands.  If bash is invoked in this
   fashion, $0 is set to the name of the file, and the positional  parame‐
   ters  are set to the remaining arguments.

In short, $0's behaviour varies depending on how bash is invoked.

alienth
  • 2,197