5
$ bash -c 'echo $0 ' foo 
foo

$ bash -c 'echo $0 '  
bash

From bash manual

($0) Expands to the name of the shell or shell script. This is set at shell initialization.

If Bash is invoked with a file of commands (see Section 3.8 [Shell Scripts], page 39), $0 is set to the name of that file.

If Bash is started with the -c option (see Section 6.1 [Invoking Bash], page 80), then $0 is set to the first argument after the string to be executed, if one is present.

Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.

What does "otherwise" mean?

What cases does it include?

Does it include the case when bash is started with -c without any "argument after the string to be executed"?

I expected bash -c 'echo $0 ' not to output anything, according to the second case in the quote, but it outputs bash.

Thanks.

1
  • "then $0 is set to the first argument after the string to be executed, if one is present."
    – wisbucky
    Nov 2, 2018 at 19:10

3 Answers 3

9

The documentation you quote gives three cases:

If bash is invoked with a file of commands, $0 is set to the name of that file.

(case 1)

If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present.

(case 2; note the two "if"s, which must both be satisfied in this case)

Otherwise, it is set to the filename used to invoke bash, as given by argument zero.

(case 3).

The "otherwise" clause covers any situation which isn't covered by cases 1 and 2: bash isn't invoked with a file of commands, and bash isn't started with the -c option, or it's started with the -c option but without any argument after the string to be executed.

So yes, it includes the case where Bash is started with -c without any argument after the string to be executed. It also includes the basic echo $0 case when run from an interactive shell, since the interactive shell was most likely started without either a file of commands or a -c option.

7
  • Thanks. What other cases does it also include?
    – Tim
    Jun 28, 2016 at 12:47
  • if I run echo $0 in a bash shell, it outputs bash. This case doesn't belong to the "otherwise" case under your interpretation, because it is not executed with bash -c. So does the "otherwise" case cover case of running a command directly in a bash shell?
    – Tim
    Jun 28, 2016 at 18:00
  • @Tim I see your point, the "otherwise" case covers more than I thought; let me rework my answer a little... Jun 28, 2016 at 18:18
  • $ bash -c "echo $0" test outputs -bash could you please clarify this behavior with double quotes ?
    – Niloct
    Apr 8, 2021 at 13:59
  • 1
    @Niloct that’s exactly it. The OP used single quotes because they wanted to test what happened in the subshell, so they ensured that the expansion wouldn’t happen in the current shell. Apr 8, 2021 at 14:37
5

You can have a test to understand manual better; suppose I have Bash under /bin. I use absolute path to call bash with -c flag as follows:

/bin/bash -c 'echo $0'

output will be /bin/bash since I have used /bin/bash as the name to invoke Bash. If I just use bash -c 'echo $0' (/bin is in my $PATH) then the output is only bash which is as expected.

1

The bash manual doesn't describe what happens in this case, as far as I can find. What happens with bash -c COMMAND with no argument to assign to $0 is that $0 is set to the filename used to invoke bash, same as when there is neither a script nor a -c option.

This is standard behavior, codified by POSIX.

For the meaning of “otherwise”, consult an English dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.