在shell脚本中使用shebang

时间:2022-02-27 21:35:31

In Linux, we usually add a shebang in a script to invoke the respective interpreter. I tried the following example.

在Linux中,我们通常在脚本中添加一个shebang来调用相应的解释器。我尝试了以下示例。

I wrote a shell script without a shebang and with executable permission. I was able to execute it using ./. But if I write a similar python program, without shebang, I am not able to execute it.

我写了一个没有shebang和可执行权限的shell脚本。我能够使用./来执行它。但是如果我写一个类似的python程序,没有shebang,我就无法执行它。

Why is this so? As far as my understanding, shebang is required to find the interpreter. So how does shell scripts work, but not a python script?

为什么会这样?据我所知,shebang需要找到翻译。那么shell脚本是如何工作的,而不是python脚本呢?

5 个解决方案

#1


2  

There’a subtle distinction here. If the target is a binary or begins with a #! shebang line, then the shell calls execv successfully. If the target is a text file without a shebang, then the call to execv will fail, and the shell is free to try launching it under /bin/sh or something else.

这里有一个微妙的区别。如果目标是二进制文件或以#开头! shebang line,然后shell调用execv成功。如果目标是没有shebang的文本文件,则对execv的调用将失败,并且shell可以*尝试在/ bin / sh或其他内容下启动它。

#2


5  

My assumption is that a script without a shebang is executed in the current environment, which at the command line is your default shell, e.g. /bin/bash.

我的假设是在当前环境中执行没有shebang的脚本,在命令行中它是你的默认shell,例如/斌/庆典。

#3


4  

shell scripts will only work if you are in the shell you targeted ... there is not python shell ... as such python will never work without explicity calling python (via shebang or command line)

shell脚本只有在你所针对的shell中才有效...没有python shell ...因为如果没有明确调用python(通过shebang或命令行),python将永远不会工作

#4


3  

By default the shell will try to execute the script. The #! notation came later

默认情况下,shell将尝试执行脚本。 #!记法来得晚了

#5


0  

http://en.wikipedia.org/wiki/Shebang_(Unix)

http://en.wikipedia.org/wiki/Shebang_(Unix)

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.

在类Unix操作系统下,当一个带有shebang的脚本作为程序运行时,程序加载器会将脚本初始行的其余部分解析为解释器指令;而是运行指定的解释器程序,将其作为参数传递给尝试运行脚本时最初使用的路径。

Now when the #! is not found, every line is interpreted as native shell command. And hence if you write a bash script and run it under bash shell it will work. If you run the same bash script in say a tcsh shell it will not work without the initial #!/usr/bin/tcsh

现在当#!找不到,每一行都被解释为本机shell命令。因此,如果你编写一个bash脚本并在bash shell下运行它会起作用。如果在tcsh shell中运行相同的bash脚本,如果没有初始#!/ usr / bin / tcsh,它将无法工作

#1


2  

There’a subtle distinction here. If the target is a binary or begins with a #! shebang line, then the shell calls execv successfully. If the target is a text file without a shebang, then the call to execv will fail, and the shell is free to try launching it under /bin/sh or something else.

这里有一个微妙的区别。如果目标是二进制文件或以#开头! shebang line,然后shell调用execv成功。如果目标是没有shebang的文本文件,则对execv的调用将失败,并且shell可以*尝试在/ bin / sh或其他内容下启动它。

#2


5  

My assumption is that a script without a shebang is executed in the current environment, which at the command line is your default shell, e.g. /bin/bash.

我的假设是在当前环境中执行没有shebang的脚本,在命令行中它是你的默认shell,例如/斌/庆典。

#3


4  

shell scripts will only work if you are in the shell you targeted ... there is not python shell ... as such python will never work without explicity calling python (via shebang or command line)

shell脚本只有在你所针对的shell中才有效...没有python shell ...因为如果没有明确调用python(通过shebang或命令行),python将永远不会工作

#4


3  

By default the shell will try to execute the script. The #! notation came later

默认情况下,shell将尝试执行脚本。 #!记法来得晚了

#5


0  

http://en.wikipedia.org/wiki/Shebang_(Unix)

http://en.wikipedia.org/wiki/Shebang_(Unix)

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.

在类Unix操作系统下,当一个带有shebang的脚本作为程序运行时,程序加载器会将脚本初始行的其余部分解析为解释器指令;而是运行指定的解释器程序,将其作为参数传递给尝试运行脚本时最初使用的路径。

Now when the #! is not found, every line is interpreted as native shell command. And hence if you write a bash script and run it under bash shell it will work. If you run the same bash script in say a tcsh shell it will not work without the initial #!/usr/bin/tcsh

现在当#!找不到,每一行都被解释为本机shell命令。因此,如果你编写一个bash脚本并在bash shell下运行它会起作用。如果在tcsh shell中运行相同的bash脚本,如果没有初始#!/ usr / bin / tcsh,它将无法工作