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
- SH is typically set to bash's soft chain
[email protected] cy]$ ll/bin/sh
lrwxrwxrwx 1 root root 4 Nov 2006/bin/sh
- In a typical Linux system (such as Redhat), using the SH call execution script is equivalent to opening the POSIX standard mode of bash
- 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.