Linux run the script and what is the difference with sh ./?

  A very interesting example:

  sh is a shell. Run sh a.sh, it means I use to explain this sh script; if I run directly ./a.sh, the first thing you will find the first line of the script specifies whether the interpreter, if not specified, will use the current system default shell (most linux default is bash), if the interpreter is specified, then the script to specify the interpreter.

For example a.run content of the document is this:

#!/usr/bin/python
print("This is Python script")

  So if you run ./a.run, the result is the output of a line of text, but if you run sh a.run, will complain

[feishu@localhost tmp]$ ./a.run
This is Python Script
[feishu@localhost tmp]$ sh a.run
a.run: line 2: syntax error near unexpected token `"This is Python Script"'
a.run: line 2: `print("This is Python Script")'

  Because this is a python script, sh read (Note that in linux suffix is usually not very strict, .run suffix is arbitrary named).
  You are writing a shell script, and ask ./a.sh sh a. sh difference, this depends on your default shell and scripting, and if you write a sh (Bourne shell) script and the first line of the interpreter to declare sh, so no difference, but if you write a bash ( Bourne Again SHell) script, the result may differ, because some of the built-in command bash and sh, and grammar are not the same.
-------------------------------------------------- --Add---------------------------------------------- ------
  my test environment is redhat 7.4

[root@localhost ~]# ls -al `which sh`
lrwxrwxrwx. 1 root root 4 6月 21 2018 /usr/bin/sh -> bash

[feishu@localhost ~]$ echo $SHELL
/bin/bash

  You can see sh is actually bash, then in this release, you if you write a shell script, but the script did not declare the first line shell interpreter, use and sh a.sh not ./a.sh What is the difference

  This switched: https://www.cnblogs.com/leanfish/p/9956516.html

Guess you like

Origin www.cnblogs.com/despotic/p/11114818.html