在 bash 脚本中,什么是#!/bin/bash
第一行是什么意思?
UPDATE: 有区别吗#!/bin/bash
and #!/bin/sh
?
这就是所谓的shebang http://en.wikipedia.org/wiki/Shebang_%28Unix%29,它告诉 shell 在执行时用什么程序解释脚本。
在您的示例中,该脚本将由bash http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29 shell.
其他一些 shebang 的例子是:
(来自维基百科)
#!/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 语法编写代码;而在带有expect shebang的脚本中,您可以使用expect语法对其进行编码,依此类推。
对更新部分的回应:
这取决于什么/bin/sh
实际上指向您的系统。通常它只是一个符号链接/bin/bash
。有时可移植脚本是用#!/bin/sh
只是为了表明它是一个 shell 脚本,但它使用所引用的任何 shell/bin/sh
在那个特定的系统上(也许它指向/bin/bash
, /bin/ksh
or /bin/zsh
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)