When is the argv[0 in Bash?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.