“#!/bin/sh”行表示什么?在 UNIX shell 脚本中是什么意思?

发布于 2024-12-04 00:17:41 字数 147 浏览 1 评论 0原文

我正在浏览一些 shell 脚本教程,发现了以下示例程序:

#!/bin/sh
clear
echo "HELLO WORLD"

谁能告诉我开头的注释 #!/bin/sh 的意义是什么?

I was going through some shell script tutorials and found the following sample program:

#!/bin/sh
clear
echo "HELLO WORLD"

Can anyone please tell me what the significance of the comment #!/bin/sh at the start is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野の 2024-12-11 00:17:41

它称为 shebang,并告诉父 shell 应使用哪个解释器来执行脚本。

#!/bin/sh <--------- bourne shell compatible script
#!/usr/bin/perl  <-- perl script
#!/usr/bin/php  <--- php script
#!/bin/false <------ do-nothing script, because false returns immediately anyways.

大多数脚本语言倾向于将#开头的行解释为注释,并忽略以下!/usr/bin/whatever< /code> 部分,否则可能会导致解释语言出现语法错误。

It's called a shebang, and tells the parent shell which interpreter should be used to execute the script.

#!/bin/sh <--------- bourne shell compatible script
#!/usr/bin/perl  <-- perl script
#!/usr/bin/php  <--- php script
#!/bin/false <------ do-nothing script, because false returns immediately anyways.

Most scripting languages tend to interpret a line starting with # as comment and will ignore the following !/usr/bin/whatever portion, which might otherwise cause a syntax error in the interpreted language.

£冰雨忧蓝° 2024-12-11 00:17:41

当您尝试在 unix 中执行程序(设置了可执行位的程序)时,操作系统将查看文件的前几个字节。这些形成了所谓的“魔数”,可以用来决定程序的格式以及如何执行它。

#! 对应于幻数 0x2321(在 ascii 表中查找)。当系统看到这个幻数时,它知道它正在处理文本脚本并读取直到下一个 \n (有一个限制,但它逃脱了我的 atm)。识别出解释器(shebang 之后的第一个参数)后,它将调用解释器。

其他文件也有幻数。尝试通过 less 查看位图 (.BMP) 文件,您将看到前两个字符是 BM。这个神奇的数字表示该文件确实是一个位图。

When you try to execute a program in unix (one with the executable bit set), the operating system will look at the first few bytes of the file. These form the so-called "magic number", which can be used to decide the format of the program and how to execute it.

#! corresponds to the magic number 0x2321 (look it up in an ascii table). When the system sees that the magic number, it knows that it is dealing with a text script and reads until the next \n (there is a limit, but it escapes me atm). Having identified the interpreter (the first argument after the shebang) it will call the interpreter.

Other files also have magic numbers. Try looking at a bitmap (.BMP) file via less and you will see the first two characters are BM. This magic number denotes that the file is indeed a bitmap.

天荒地未老 2024-12-11 00:17:41

如果该脚本所在的文件是可执行的,则哈希符号 (#!) 会告诉操作系统使用哪个解释器来运行该脚本。例如,在本例中为 /bin/sh

有关详细信息,有一篇 维基百科文章

If the file that this script lives in is executable, the hash-bang (#!) tells the operating system what interpreter to use to run the script. In this case it's /bin/sh, for example.

There's a Wikipedia article about it for more information.

茶底世界 2024-12-11 00:17:41

第一行告诉 shell,如果直接执行脚本(./run.sh;而不是 /bin/sh run.sh),它应该使用该程序(在本例中为 /bin/sh)来解释它。

您还可以使用它来传递参数,通常是 -e(出错时退出),或使用其他程序(/bin/awk、/usr/bin/perl 等)。

The first line tells the shell that if you execute the script directly (./run.sh; as opposed to /bin/sh run.sh), it should use that program (/bin/sh in this case) to interpret it.

You can also use it to pass arguments, commonly -e (exit on error), or use other programs (/bin/awk, /usr/bin/perl, etc).

太阳男子 2024-12-11 00:17:41

#!/bin/sh#!/bin/bash 必须是脚本的第一行,因为如果您不在第一行使用它,那么系统将会将该脚本中的所有命令视为不同的命令。如果第一行是#!/bin/sh,那么它将把所有命令视为一个脚本,并且会显示该文件正在 ps 命令中运行,而不是在文件内的命令。

./echo.sh

ps -ef |grep echo
trainee   3036  2717  0 16:24 pts/0    00:00:00 /bin/sh ./echo.sh
root      3042  2912  0 16:24 pts/1    00:00:00 grep --color=auto echo

#!/bin/sh or #!/bin/bash has to be first line of the script because if you don't use it on the first line then the system will treat all the commands in that script as different commands. If the first line is #!/bin/sh then it will consider all commands as a one script and it will show the that this file is running in ps command and not the commands inside the file.

./echo.sh

ps -ef |grep echo
trainee   3036  2717  0 16:24 pts/0    00:00:00 /bin/sh ./echo.sh
root      3042  2912  0 16:24 pts/1    00:00:00 grep --color=auto echo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文