Each C program has a main function, and each main function has a argv parameter, which is an array of strings, the value of which is specified by the parent process of the C program when it is started by the exec* function.
A lot of people say that the value of the max in Bash is the value of argv[0] (zeroth argument) that the C program obtains in its main function, and we can demonstrate it through the function of the-a parameter of the EXEC command:
$ (exec-a foo bash-c ' echo $ ') Foo $ (exec-a ... bash-c ' echo-$ ') ... $ (exec-a "" Bash-c ' echo $ ') |
But this is not always the case, in two cases, the value of $ is not argv[0]:
Bash-c ' ... ' foo bar ...
$ Bash-c ' echo $ $ ' foo bar Foo Bar |
This time the Bash program's argv[0] is "bash", but the "foo" is. That is, if the parameter of the-C option is followed by a parameter, then those parameters will be added in turn (overwriting the old value argv[0]), $, $ ....
bash/a/b/c.sh
$ cat foo.sh Echo $ bash foo.sh foo.sh $ bash./foo.sh ./foo.sh $./foo.sh ./foo.sh |
This time the Bash program's argv[0] or "bash", but the "foo.sh". In other words, when executing a script, the value of $ A is the relative or absolute path of the script (any legal path you specify). You enter./foo.sh on the command line as well, because the operating system will perform/bin/sh for you./foo.sh.
As for the three cases of the value of $, the Bash documentation actually tells me that I have three different colors to annotate related words:
($) Expands to the name of the shell or shell script. This was set at shell initialization. If Bash is invoked with a file of commands (see Shell Scripts), was set to the name of the $0
file. C4>if Bash -c is started with the option (see invoking Bash), then was set to the first $0
argument After the string to was executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.
When is the argv[0 in Bash?