首页 文章

Bash脚本:#!/ bin / bash是什么意思? [重复]

提问于
浏览
87

这个问题在这里已有答案:

在bash脚本中,第一行的 #!/bin/bash 是什么意思?

UPDATE#!/bin/bash#!/bin/sh 之间有区别吗?

3 回答

  • 105

    这被称为shebang,它告诉shell在执行时用什么程序来解释脚本 .

    在您的示例中,脚本将由bash shell解释和运行 .

    其他一些例子是:

    (来自维基百科)

    #!/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
    

    还有一些我能想到的额外的东西,例如:

    #!/bin/ksh
    #!/bin/awk
    #!/bin/expect
    

    例如,在使用bash shebang的脚本中,您将使用bash语法编写代码;而在期望shebang的脚本中,您可以使用expect语法对其进行编码,依此类推 .

    Response to updated portion:

    这取决于 /bin/sh 在您的系统上实际指向的内容 . 通常它只是 /bin/bash 的符号链接 . 有时可移植脚本是用 #!/bin/sh 编写的,只是为了表示它是一个shell脚本,但是它使用 /bin/sh 在该特定系统上引用的任何shell(可能它指向 /bin/bash/bin/ksh/bin/zsh

  • 12

    In bash script, what does #!/bin/bash at the 1st line mean ?

    在Linux系统中,我们有shell来解释我们的UNIX命令 . 现在Unix系统中有很多shell . 其中,有一个名为bash的shell,这是一个非常常见的Linux,它有着悠久的历史 . 这是Linux中的默认shell .

    当您编写脚本(unix命令的集合等)时,您可以选择指定可以使用的shell . 通常,您可以使用Shebang(是's what it'的名称)指定它将成为哪个shell .

    因此,如果你在脚本的顶部#!/ bin / bash,那么你告诉你的系统使用bash作为默认shell .

    Now coming to your second question :Is there a difference between #!/bin/bash and #!/bin/sh ?

    答案是肯定的 . 当你告诉#!/ bin / bash然后你告诉你的环境/ os使用bash作为命令解释器 . 这是硬编码的东西 .

    每个系统都有自己的shell,系统将使用它来执行自己的系统脚本 . 这个系统shell可以在OS到OS之间变化(大部分时间都是bash .Ubuntu最近使用dash作为默认系统shell) . 当您指定#!/ bin / sh时,系统将使用它的内部系统shell来解释您的shell脚本 .

    请访问此link以获取我已解释此主题的更多信息 .

    希望这会消除你的困惑......祝你好运 .

  • 12

    当脚本中的第一个字符是 #! 时,称为shebang . 如果您的文件以 #!/path/to/something 开头,则标准是运行 something 并将该文件的其余部分作为输入传递给该程序 .

    话虽如此, #!/bin/bash#!/bin/sh 或甚至 #!/bin/zsh 之间的区别在于是否使用bash,sh或zsh程序来解释文件的其余部分 . 传统上, bashsh 只是不同的程序 . 在某些Linux系统上,它们是同一程序的两个副本 . 在其他Linux系统上, shdash 的链接,在传统的Unix系统(Solaris,Irix等)上 bash 通常是与 sh 完全不同的程序 .

    当然,该线的其余部分不必以sh结尾 . 它也可以是 #!/usr/bin/python#!/usr/bin/perl ,甚至 #!/usr/local/bin/my_own_scripting_language .

相关问题