Shell SH Bash concept

Source: Internet
Author: User
Tags posix

There is often a sentence at the beginning of a shell script to define what kind of SH interpreter to use to interpret the script.
There are two main types of shell scripts that are currently developed for testing:
(1) #!/bin/sh
(2) #!/bin/bash
What is the difference between the two ways? What are the different effects of 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 unsuccessful and does not run code after the source.
Modify the first line of the test.sh script, change to #!/bin/bash, and execute 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, the Echo statement after source is run.
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 after the source is not run if it is run with SH.

Why is there such a difference?

Description

1. SH is generally set to bash's soft chain
[Email protected] cy]$ ll/bin/sh
lrwxrwxrwx 1 root root 4 Nov 2006/bin/sh
2. In a typical Linux system (e.g. Redhat), using the SH call execution script is equivalent to opening the POSIX standard mode of bash
3. That is to say/bin/sh equivalent to/bin/bash--posix

So, the difference between SH and bash is actually that bash has no way to turn on POSIX mode.

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

Shell SH Bash concept

Related Article

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.