Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有谁知道为什么以下脚本有效?
#a-random-junk-string echo HI
shell 执行 echo 命令,并输出 HI。我以为既然没有“!” 在“#”之后,shell 会报错。
如果没有#! 指定特定的解释器,内核将不会拦截并使用指定的程序启动它。
但是,当前的shell可能仍将其解释为命令文件,这就是您所看到的。
当 shell 被要求在可执行位打开的情况下运行文件时,它将检查文件并确定它是否以 shebang #! 如果确实如此,那么它将执行该命令,该命令将从文件的其余部分获取它的程序文本。
如果文件不是以 shebang 开头,那么 shell 将尝试自己执行它。这就是您正在发生的事情,shell 将第一行解释为注释。