#!/bin/bash

Source: Internet
Author: User
Tags posix

In the first line of the shell script, you must write#!/bin/bashit. With this problem, I searched Baidu today for a moment,
Found in a post to discuss the issue, feel that the various followers in the answer to this question, the explanation is very reasonable, and now the note is as follows, in order to learn.
---------------------------------------------------------------------------------------------
I am a beginner of Linux soon, found that when writing a script, there is a line at the beginning of #!/bin/sh
I remember # is the annotation, then this line should be can not write it. Why do they say "This statement tells the operating system where the program is used to interpret the script。 "Then.
---------------------------------------------------------------------------------------------
#!/bin/sh
Indicates that this script is interpreted by the SH program of the/bin/path ....
With the command line ~
#/bin/sh ScriptName effect is the same ...
---------------------------------------------------------------------------------------------
But # is not used to indicate a comment. Isn't that what this is about?
---------------------------------------------------------------------------------------------
Different places, #有不同的意思
Just like C + + with "//" to annotate
VB with the "'" annotation the same
DNS inside with ";" To annotate
The beginning of the shell is not for annotations.
---------------------------------------------------------------------------------------------
If you want to use the script to execute permissions ... The first line must declare what the script interprets ...
#的确表示注释. The first line you just need to know isthe path to the declaration interpreterJust fine:)
If the first line is something else. And you give x permission, the execution will go wrong ...
Can not write. Perform manual execution ...
/bin/sh xxxx.sh
The effect is the same ...
---------------------------------------------------------------------------------------------
If you don't write it, then use the shell you landed on to explain the execution.
---------------------------------------------------------------------------------------------
Can not write, but should have good programming habits
---------------------------------------------------------------------------------------------
#通常用作注释, but #! together marks this as a shell script, followed by a path that indicates the program used to interpret the script.
If a script is just a stack of some ordinary Linux instructions. Then #! can be omitted and not written. But it's not always the case that we meet.
If the script contains some custom program components, such as functions, variables, and so on, the #! will need to be labeled.

At the beginning of the shell script there is often a #! word to define which SH interpreter to use to interpret the script.
There are two main ways to develop a shell script for testing:
(1) #!/bin/sh
(2) #!/bin/bash
Here is a question for fellow heroes of the same Fortune Inn:
What is the difference between the above two methods. What difference does it make to the actual operation of the script?

Script test.sh content:
#!/bin/sh
SOURCE pcy.sh #pcy. SH does not exist
echo Hello
Execute./test.sh, the screen output is:
./test.sh:line 2:pcy.sh:no such file or directory
Thus, in the case of #!/bin/sh, source is not successful, will not run the code after source.
Modifies the first line of the test.sh script, becomes #!/bin/bash, and executes again./test.sh, the screen output is:
./test.sh:line 2:pcy.sh:no such file or directory
Hello
Thus, in the case of #!/bin/bash, although source is unsuccessful, it still runs the Echo statement after source.
But then I tried to run the Sh./test.sh, this time the screen output is:
./test.sh:line 2:pcy.sh:no such file or directory
Indicates that although #!/bin/bash is specified in the script, if the source is not successful, the code behind source will not be run if it is run with SH.

Why is there such a difference?

Junru the students to explain

1. SH is generally set to bash the soft chain
[work@zjm-testing-app46 cy]$ ll/bin/sh
lrwxrwxrwx 1 root     root    ;       4 Nov 13   2006/bin/sh-> Bash
2. In a typical Linux system (such as Redhat), Using the SH call execution script is equivalent to opening Bash's POSIX standard mode
3. That is,/bin/sh is equivalent to/bin/bash--posix

So, the difference between SH and bash, In fact, is that bash is not turning on the difference in POSIX mode

So, it can be expected that if the first line is written as #!/bin/bash--posix, then the script execution effect is the same as #!/bin/sh (following POSIX-specific specifications, It is possible to include such a specification: "Do not continue to interpret when a line of code is wrong"

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.