Linux shell 奇怪的情况

发布于 2024-09-11 10:39:12 字数 133 浏览 2 评论 0原文

有谁知道为什么下面的脚本有效?

#a-random-junk-string
echo HI

shell执行echo命令,输出HI。我想既然没有“!”在“#”之后,shell 会报错。

Does anyone know why the following script works?

#a-random-junk-string
echo HI

The shell executes the echo command, and outputs HI. I thought that since there is no "!" after the "#", the shell would give an error.

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

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

发布评论

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

评论(2

踏月而来 2024-09-18 10:39:12

如果没有#!指定特定的解释器,内核将不会拦截并使用指定的程序启动它。

但是,当前的 shell 仍可能将其解释为命令文件,这就是您所看到的情况。

If there is no #! specifying a specific interpreter, the kernel will not intercept and launch it with the specified program.

However, the current shell may still interpret it as a command file, which is what you are seeing take place.

清醇 2024-09-18 10:39:12

当 shell 被要求运行一个打开了可执行位的文件时,它将检查该文件并确定它是否以 shebang #! 开头。如果是,那么它将执行该命令,该命令将从文件的其余部分获取其程序文本。

如果文件不是以 shebang 开头,那么 shell 将尝试自行执行它。这就是您所发生的情况,shell 将第一行解释为注释。

When the shell is asked to run a file with the executable bit turned on then it will examine the file and determine if it begins with a shebang #! if it does then it will execute that command which will get it's program text from the remainder of the file.

If the file does not start with a shebang then the shell will attempt to execute it itself. This is what is happening for you and the shell interprets the first line as a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文