shell脚本的#!

来源:互联网 发布:开淘宝店能挣钱吗 编辑:程序博客网 时间:2024/06/05 17:29

摘自shell FAQ


#! 我不是注释,我是引用解释。

A1:bash脚本开始总少不了一句

[cpp] view plain copy
 print?
  1. #! /bin/bash  
这行是脚本的sha-bang line(sharp (#) and bang (!))是要告诉大家这个脚本是由 /bin/bash来解释的

参考( Mendel Cooper 的 Advanced Bash Scripting Guide  ,第一章第二部分  Chapter 2. Starting Off With a Sha-Bang

The sha-bang ( #![1] at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [2] magic number, a special marker that designates a file type, or in this case an executable shell script (type man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (the line following the sha-bang line), and ignoring comments. [3]

#!叫做Magic number(幻数、魔数。含义为当一个普通字符含有特殊意义时就叫幻数), #! 用来指定解释脚本的程序,可以是 Shell,也可以是其他程序。

[cpp] view plain copy
 print?
  1. #!/bin/php  
  2. #!/bin/expect  
  3. #!/bin/perl  
  4. ......  

1) 如果shell脚本的第一个非空白字符不是“ #”,则它会使用Bourne shell。
2) 如果shell脚本的第一个非空白字符是“ #”,但不以“# !”开头时,则它会使用C shell。
3) 如果shell脚本以“# !”开头,则“ # !”后面所跟的字符串就是所使用的shell的绝对路径名。Bourne shell的路径名称为/bin/sh ,而C 外壳则为/bin/csh。


0 0
原创粉丝点击