脚本如何以#!/ bin/rm开头自行删除?

Dmi*_*sky 6 unix linux bash shell

我正在阅读Advanced Bash-Scripting Guide的第2章,我在脚注中读到了这个脚本:

#!/bin/rm
# Self-deleting script.

# Nothing much seems to happen when you run this... except that the file disappears.

WHATEVER=85

echo "This line will never print (betcha!)."

exit $WHATEVER  # Doesn't matter. The script will not exit here.
                # Try an echo $? after script termination.
                # You'll get a 0, not a 85.
Run Code Online (Sandbox Code Playgroud)

通常rm接受一个参数并删除该文件.在这里,rm不知何故知道对它正在执行的脚本采取行动.这是否意味着当shell遇到#!它时,它会将(完全限定的?)路径传递给该文件,作为之后指定的程序的参数#!

Ric*_*dle 3

是的,当您说“它将(完全限定?)路径传递给该文件作为 #! 之后指定的程序的参数”时,您是完全正确的。

这就是为什么 shell 脚本以#!/bin/sh或 类似的开头,而 Python 脚本以#!/usr/local/bin/python.

“shebang”行旨在运行脚本解释器,但可以指定任何可执行文件。