Shell script header, the difference between #!/bin/sh and #!/bin/bash.

Source: Internet
Author: User
Tags posix

Because today wrote a small script, dead or alive unsuccessful, always reported file or directory does not exist, asked our horse classmate's normal wording, found only the difference between the script head, that is, today this article to talk about the #!/bin/sh and #!/bin/bash.

This article references: https://www.cnblogs.com/aaronLinux/p/6885288.html and http://www.cnblogs.com/baizhantang/archive/2012/09/11/2680453.html

One: Explanation

#!/bin/sh means that this script uses/bin/sh to interpret execution, and #! is a special notation followed by the path to the shell that interprets this script.

$ cat/etc/shells can view the shell format supported by the system

In fact, the first sentence of the #! is the script interpreter program path, the content of the script is interpreted by the interpreter, we can use a variety of interpreters to write the corresponding script.

such as/bin/csh scripts,/bin/perl scripts,/bin/awk scripts,/bin/sed scripts,/bin/python scripts, even/bin/echo, and so on.

#!/bin/bash.

Two: Difference

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?

Junru explained to the students

    1. SH is typically 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 (such as Redhat), using the SH call execution script is equivalent to opening the POSIX standard mode of bash
    3. Which means/bin/sh is 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 script header, the difference between #!/bin/sh and #!/bin/bash.

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.