Bash 脚本:#!/bin/bash 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13872048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Script : what does #!/bin/bash mean?
提问by Raptor
In bash script, what does #!/bin/bash
at the 1st line mean ?
在 bash 脚本中,#!/bin/bash
第一行是什么意思?
UPDATE: Is there a difference between #!/bin/bash
and #!/bin/sh
?
更新:有没有之间的差异#!/bin/bash
和#!/bin/sh
?
回答by sampson-chen
That is called a shebang, it tells the shell what program to interpret the script with, when executed.
这称为shebang,它告诉 shell 在执行时用什么程序来解释脚本。
In your example, the script is to be interpreted and run by the bashshell.
在您的示例中,脚本将由bashshell解释和运行。
Some other example shebangs are:
其他一些示例 shebang 是:
(From Wikipedia)
(来自维基百科)
#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
#!/bin/csh — Execute the file using csh, the C shell, or a compatible shell
#!/usr/bin/perl -T — Execute using Perl with the option for taint checks
#!/usr/bin/php — Execute the file using the PHP command line interpreter
#!/usr/bin/python -O — Execute using Python with optimizations to code
#!/usr/bin/ruby — Execute using Ruby
and a few additional ones I can think off the top of my head, such as:
还有一些我能想到的,例如:
#!/bin/ksh
#!/bin/awk
#!/bin/expect
In a script with the bash shebang, for example, you would write your code with bash syntax; whereas in a script with expect shebang, you would code it in expect syntax, and so on.
例如,在带有 bash shebang 的脚本中,您将使用 bash 语法编写代码;而在带有 expect shebang 的脚本中,您可以使用 expect 语法对其进行编码,依此类推。
Response to updated portion:
对更新部分的回应:
It depends on what /bin/sh
actually points to on your system. Often it is just a symlink to /bin/bash
. Sometimes portable scripts are written with #!/bin/sh
just to signify that it's a shell script, but it uses whichever shell is referred to by /bin/sh
on that particular system (maybe it points to /bin/bash
, /bin/ksh
or /bin/zsh
)
这取决于/bin/sh
您的系统上实际指向的内容。通常它只是一个符号链接到/bin/bash
. 有时编写可移植脚本#!/bin/sh
只是为了表示它是一个 shell 脚本,但它使用/bin/sh
在该特定系统上引用的任何 shell (可能它指向/bin/bash
,/bin/ksh
或/bin/zsh
)
回答by kta
In bash script, what does #!/bin/bash at the 1st line mean ?
在 bash 脚本中,第一行的 #!/bin/bash 是什么意思?
In Linux system, we have shell which interprets our UNIX commands. Now there are a number of shell in Unix system. Among them, there is a shell called bash which is very very common Linux and it has a long history. This is a by default shell in Linux.
在 Linux 系统中,我们有 shell 来解释我们的 UNIX 命令。现在Unix系统中有许多shell。其中,有一个叫做bash的shell,在Linux中很常见,而且历史悠久。这是 Linux 中的默认 shell。
When you write a script (collection of unix commands and so on) you have a option to specify which shell it can be used. Generally you can specify which shell it wold be by using Shebang(Yes that's what it's name).
当您编写脚本(unix 命令的集合等)时,您可以选择指定可以使用哪个 shell。通常,您可以使用Shebang指定它是哪个外壳(是的,这就是它的名字)。
So if you #!/bin/bash in the top of your scripts then you are telling your system to use bash as a default shell.
因此,如果您在脚本顶部使用 #!/bin/bash,那么您就是在告诉您的系统使用 bash 作为默认 shell。
Now coming to your second question :Is there a difference between #!/bin/bash and #!/bin/sh ?
现在来到你的第二个问题: #!/bin/bash 和 #!/bin/sh 之间有区别吗?
The answer is Yes. When you tell #!/bin/bash then you are telling your environment/ os to use bash as a command interpreter. This is hard coded thing.
答案是肯定的。当您告诉 #!/bin/bash 时,您就是在告诉您的环境/操作系统使用 bash 作为命令解释器。这是硬编码的东西。
Every system has its own shell which the system will use to execute its own system scripts. This system shell can be vary from OS to OS(most of the time it will be bash. Ubuntu recently using dashas default system shell). When you specify #!/bin/sh then system will use it's internal system shell to interpreting your shell scripts.
每个系统都有自己的 shell,系统将使用它来执行自己的系统脚本。这个系统 shell 可能因操作系统而异(大多数时候它是 bash。Ubuntu 最近使用dash作为默认系统 shell)。当您指定 #!/bin/sh 时,系统将使用它的内部系统 shell 来解释您的 shell 脚本。
Visit this linkfor further information where I have explained this topic.
访问此链接以获取更多信息,我已在其中解释了此主题。
Hope this will eliminate your confusions...good luck.
希望这会消除你的困惑......祝你好运。
回答by Joshua D. Boyd
When the first characters in a script are #!
, that is called the shebang. If your file starts with
#!/path/to/something
the standard is to run something
and pass the rest of the file to that program as an input.
当脚本中的第一个字符是 时#!
,这称为shebang。如果您的文件以#!/path/to/something
标准开头,
则运行something
并将文件的其余部分作为输入传递给该程序。
With that said, the difference between #!/bin/bash
, #!/bin/sh
, or even #!/bin/zsh
is whether the bash, sh, or zsh programs are used to interpret the rest of the file. bash
and sh
are just different programs, traditionally. On some Linux systems they are two copies of the same program. On other Linux systems, sh
is a link to dash
, and on traditional Unix systems (Solaris, Irix, etc) bash
is usually a completely different program from sh
.
话虽如此,#!/bin/bash
、#!/bin/sh
、 甚至之间的区别在于#!/bin/zsh
是否使用 bash、sh 或 zsh 程序来解释文件的其余部分。 bash
和sh
传统上只是不同的程序。在某些 Linux 系统上,它们是同一个程序的两个副本。在其它Linux系统,sh
是一个链接dash
,并在传统的Unix系统(Solaris和Irix的,等等)bash
是从通常是完全不同的程序sh
。
Of course, the rest of the line doesn't have to end in sh. It could just as well be #!/usr/bin/python
, #!/usr/bin/perl
, or even #!/usr/local/bin/my_own_scripting_language
.
当然,该行的其余部分不必以 sh 结尾。它也可以是#!/usr/bin/python
,#!/usr/bin/perl
, 甚至#!/usr/local/bin/my_own_scripting_language
。