To add a bit more information to @Mohit's good answer:
要向@ Mohit的好答案添加更多信息:
Unix uses many interpreters for many languages. Some of them are called "shells", but most are just another computer language to the system. In fact, every file is written in some language, even if it's compiled assembly of Java bytecodes.
Unix为许多语言使用了许多解释器。其中一些被称为“shell”,但大多数只是系统的另一种计算机语言。实际上,每个文件都是用某种语言编写的,即使它是Java字节码的编译汇编。
The first few bytes of a file are "magic": they tell the OS how to execute the file. If the first two bytes are '#!', the OS knows that the file needs an interpreter. The rest of the first line up to newline is then used as a command to execute. The first "word" (space-separated group of non-spaces) of the line is interpreted as absolute file name to run, and all the other words are passed to it as command line arguments. Last parameter is the file name of the file you're running.
文件的前几个字节是“神奇的”:它们告诉操作系统如何执行文件。如果前两个字节是'#!',则OS知道该文件需要解释器。然后将第一行到换行符的其余部分用作执行命令。该行的第一个“单词”(以空格分隔的非空格组)被解释为要运行的绝对文件名,所有其他单词作为命令行参数传递给它。最后一个参数是您正在运行的文件的文件名。
So, for example, if you have the first line as
所以,例如,如果你有第一行
#!/bin/tclsh
in a file /home/user/aaa.tcl
在文件/home/user/aaa.tcl中
the OS will execute /bin/tclsh with /home/user/aaa.tcl as command line argument:
操作系统将使用/home/user/aaa.tcl作为命令行参数执行/ bin / tclsh:
/bin/tclsh /home/user/aaa.tcl
For a more advanced example, try this:
有关更高级的示例,请尝试以下操作:
#! /bin/env perl
in /home/user/myperlscript
This executes the following command:
这将执行以下命令:
/bin/env perl /home/user/myperlscript
/bin/env is a utility program that looks up its first argument using PATH environment variable, and then executes the program it finds, passing the rest of its arguments on to the program. With the help of env, you can use PATH to find your interpreters.
/ bin / env是一个实用程序,它使用PATH环境变量查找其第一个参数,然后执行它找到的程序,将其余的参数传递给程序。在env的帮助下,您可以使用PATH找到您的口译员。