原博文

使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗?

首先,这个符号(#!)的名称,叫做"Shebang"或者"Sha-bang"。

Linux执行文件时发现这个格式,会把!后的内容提取出来拼接在脚本文件或路径之前,当作实际执行的命令。

Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件的解释程序。

1. 如果脚本文件中没有#!这一行,那么它执行时会默认用当前Shell去解释这个脚本(即:$SHELL环境变量)。

2. 如果#!之后的解释程序是一个可执行文件,那么执行这个脚本时,它就会把文件名及其参数一起作为参数传给那个解释程序去执行。

3. 如果#!指定的解释程序没有可执行权限,则会报错“bad interpreter: Permission denied”。
    如果#!指定的解释程序不是一个可执行文件,那么指定的解释程序会被忽略,转而交给当前的SHELL去执行这个脚本。

4. 如果#!指定的解释程序不存在,那么会报错“bad interpreter: No such file or directory”。
    注意:#!之后的解释程序,需要写其绝对路径(如:#!/bin/bash),它是不会自动到$PATH中寻找解释器的。

5. 当然,如果你使用"bash test.sh"这样的命令来执行脚本,那么#!这一行将会被忽略掉,解释器当然是用命令行中显式指定的bash。

例如:test.sh

Shell
#!/bin/bash
echo "hello, world."
echo "hello, ${1}."
1
2
3
#!/bin/bash
echo "hello, world."
echo "hello, ${1}."

chmod a+x  test.sh

./test.sh   Jay  (运行之时,其实是 /bin/bash ./test.sh Jay)

结果为:

hello, world.
hello, Jay.

Some typical shebang lines:

  • #!/bin/sh – Execute the file using the Bourne shell, or a compatible shell, with path /bin/sh
  • #!/bin/bash – Execute the file using the Bash shell.
  • #!/bin/csh -f – Execute the file using csh, the C shell, or a compatible shell, and suppress the execution of the user’s .cshrc file on startup
  • #!/usr/bin/perl -T – Execute using Perl with the option for taint checks
  • #!/usr/bin/env python – Execute using Python by looking up the path to the Python interpreter automatically via env
  • #!/bin/false – Do nothing, but return a non-zero exit status, indicating failure. Used to prevent stand-alone execution of a script file intended for execution in a specific context, such as by the . command from sh/bash, source from csh/tcsh, or as a .profile, .cshrc, or .login file.

主要参考资料:

http://en.wikipedia.org/wiki/Shebang_(Unix)

http://people.csail.mit.edu/jaffer/Docupage/sharpbang.html

Shebang(#!)[转]的更多相关文章

  1. (转)Linux上的Shebang符号(#!)

    https://linux.cn/article-3664-1.html 本文了将给你简单介绍一下Shebang(”#!”)这个符号. 首先,这个符号(#!)的名称,叫做”Shebang”或者”Sha ...

  2. [linux-脚本]shebang(shabang #!)

    使用Linux或者unix系统的人们对#!这个符号都不陌生,但要说出个具体的所以然来,很多人估计还真不行,我们有必要就此整理一下.Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指 ...

  3. Bash Shebang 小结

    在 shell(Bash 是一种 shell) 中执行外部程序和脚本时,Linux 内核会启动一个新的进程,以便在新的进程中执行指定的程序或脚本.内核知道该如何为编译型的程序做这件事,但是对于脚本程序 ...

  4. 00001 - Linux 上的 Shebang 符号(#!)

    使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗? 本文了将给你简单介绍一下Shebang(”#!”)这个符号. 首先,这个符号(#!)的名称,叫做”Shebang ...

  5. 看懂shebang吧,只需一点点shell知识,从此再也不犯强迫症

    Python2: 开启一个terminal,输入下面命令: yshuangj@ubuntu:~$ vim helloA.py 在vim编辑器中,进入编辑模式(按i),输入下面的代码,然后退出编辑模式( ...

  6. 释伴:Linux 上的 Shebang 符号(#!)

    使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗? 本文了将给你简单介绍一下Shebang(”#!”)这个符号. 首先,这个符号(#!)的名称,叫做”Shebang ...

  7. shebang是啥

    在计算领域中,Shebang(也称为 Hashbang )是一个由井号和叹号构成的字符序列 #! ,其出现在文本文件的第一行的前两个字符. 在文件中存在 Shebang 的情况下,类 Unix 操作系 ...

  8. 人们为什么在Python脚本的第一行上编写#!/ usr / bin / env python shebang?

    在我看来,如果没有该行,文件运行相同. #1楼 您可以使用virtualenv尝试此问题 这是test.py #! /usr/bin/env python import sys print(sys.v ...

  9. Shell脚本中的shebang到底是什么

    使用类Unix系统的同学可能都对"#!"这个符号并不陌生,但是你真的了解它吗? 这个符号的名称,叫做"Shebang"或者"Sha-bang" ...

随机推荐

  1. JavaFx之不通过全局静态变量进行窗体通信

    百度了n多窗体通信,,,总是通过定义全局静态变量进行传值通信..我个人不喜欢一个controller里写满所有的布局(这样显得臃肿,但是组件传值方便).有没有另外的办法进行模块化并且可以传值呢.. 肯 ...

  2. Session帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  3. Scala学习(八)---Scala继承

    Scala继承 摘要: 在本篇中,你将了解到Scala的继承与Java和C++最显著的不同.要点包括: 1. extends.final关键字和Java中相同 2. 重写方法时必须用override ...

  4. Sql_连接查询中on筛选与where筛选的区别

    sql中的连接查询分为3种, cross join,inner join,和outer join ,  在 cross join和inner join中,筛选条件放在on后面还是where后面是没区别 ...

  5. 基于RC4加密算法的图像加密

    基于RC4加密算法的图像加密 某课程的一个大作业内容,对图像加密.项目地址:https://gitee.com/jerry323/RC4_picture 这里使用的是RC4(流.对称)加密算法,算法流 ...

  6. /var/lib/mysql 的访问权限问题 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    mysql 登录不进去 提示Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) she ...

  7. Eclipse布局问题小记

    当Eclipse的Debug,Console(简称工具条)页面被误操作到占据整行时,通过点击工具条的非选项卡部分,然后向代码区域拖动,即可得恢复非单独行模式.

  8. MyBatis 集合操作语法范例:配合SQL的in关键字

    Java语法: private String[] tagIds; MyBatis语法 <delete id="deleteByIds" parameterType=" ...

  9. ubuntu安装steam

    增加第三方自由库的软件支持 sudo add-apt-repository multiverse 增加更新支持包 sudo add-apt-repository multiverse 安装steam ...

  10. python Thread、lock

    Python的标准库提供了两个模块:_thread和threading,_thread是低级模块,threading是高级模块,对_thread进行了封装.绝大多数情况下,我们只需要使用threadi ...