Linux shell 奇怪的情况
有谁知道为什么下面的脚本有效?
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有#!指定特定的解释器,内核将不会拦截并使用指定的程序启动它。
但是,当前的 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.
当 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.