#!/bin/bash是指此脚本使用/bin/bash来解释执行。
其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径。
bash只是shell的一种,还有很多其它shell,如:sh,csh,ksh,tcsh,...
我们可以通过以下一个示例来进行实验,了解#!/bin/bash的使用。
1)#!/bin/bash只能放在第一行,如果后面还有#!,那么只能看成是注释。
这里有三个脚本(脚本都要使用”chmod +x  scriptname“命令来获得可执行权限):
tbash1.sh:
#!/bin/sh
source abc
echo "hello abc"
 
tbash2.sh:
#!/bin/bash
source abc
echo "hello abc"
 
tbash3.sh:
source abc
echo "hello abc"
 
三个脚本执行的结果:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 2: abc: No such file or directory
注:当source命令执行有问题时,sh不再往下面执行。
[nsvc@localhost other]$ ./tbash2.sh 
./tbash2.sh: line 2: abc: No such file or directory
hello abc
注:当source命令执行有问题时,bash继续执行下面命令。
[nsvc@localhost other]$ ./tbash3.sh 
./tbash3.sh: line 1: abc: No such file or directory
hello abc
注:自身登录系统所在的shell是bash。所以,当source命令执行有问题时,bash继续执行下面命令。
 
如果将tbash1.sh改成:
echo "abc"
#!/bin/sh
source abc
echo "hello abc"
那么,执行结果是:
[nsvc@localhost other]$ ./tbash1.sh 
abc
./tbash1.sh: line 3: abc: No such file or directory
hello abc
也就是说,脚本忽略了第二行“#!/bin/sh",直接使用当前所在的shell(也就是bash)来解释脚本。
 
当把tbash1.sh改成:
#!/bin/sh
#!/bin/bash
source abc
echo "hello abc"
执行结果为:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 3: abc: No such file or directory
当执行完source命令时,并没有往下执行。说明,#!/bin/sh这一行起到作用了,但#!/bin/bash并没有起作用。在脚本中,不在第一行的#!/bin/bash,只是一个注释。
 
2)#!后面的路径一定要正确,不正确会报错。
假如,我们把tbash1.sh中第一行的#!后面加了一个不存在的路径”/home/sh“:
#!/home/sh
source abc
echo "hello abc"
执行结果为:
[nsvc@localhost other]$ ./tbash1.sh 
-bash: ./tbash1.sh: /home/sh: bad interpreter: No such file or directory
系统会提示/home/sh的路径不存在。
 
3)如果一个脚本在第一行没有加上#!+shell路径这一行,那么,脚本会默认当前用户登录的shell,为脚本解释器。
在1)中,脚本tbash3.sh的执行结果,就是用当前自己登录的shell(bash)解释后的结果。我们通常所用的shell都是bash,如果哪天登录到sh,再使用以上类型的脚本,就会有问题。以下是自己登录到sh下,执行tbash3.sh的结果:
-sh-3.2$ ./tbash3.sh 
./tbash3.sh: line 1: abc: 没有那个文件或目录
与1)中的执行结果是不一样的。
因此,大家应该养成脚本首行加上#!+shell路径的习惯。
 
4)/bin/sh相当于/bin/bash --posix
我们将脚本tbash1.sh改为:
#!/bin/bash --posix
source abc
echo "hello abc"
执行结果:
[nsvc@localhost other]$ ./tbash1.sh 
./tbash1.sh: line 2: abc: No such file or directory
与tbash1.sh原脚本执行的结果一样。
 
我们还可以以tbash3.sh为示例。
用以下命令来执行该脚本:
[nsvc@localhost other]$ bash tbash3.sh
tbash3.sh: line 1: abc: No such file or directory
hello abc
[nsvc@localhost other]$ sh tbash3.sh 
tbash3.sh: line 1: abc: No such file or directory
[nsvc@localhost other]$ bash --posix tbash3.sh 
tbash3.sh: line 1: abc: No such file or directory
 "bash tbash3.sh"表示使用bash来作为脚本解释器来执行tbash3.sh。同样,也可以使用如”sh 脚本名“这样的命令,来用sh作为脚本解释器。
从结果可以看出,/bin/bash --posix与/bin/sh的执行结果相同。总结起来,sh跟bash的区别,实际上是bash有没开启posix模式的区别。遵守posix规范,可能包括,”当某行代码出错时,不继续往下执行。“
 
最后加上一点说明,每个脚本开头都使用"#!",#!实际上是一个2字节魔法数字,这是指定一个文件类型的特殊标记,在这种情况下,指的就是一个可执行的 脚本。在#!之后,接一个路径名,这个路径名指定了一个解释脚本命令的程序,这个程序可以是shell,程序语言或者任意一个通用程序。
 
总结起来,要规规举举地按照秩序行。

#!/bin/bash的更多相关文章

  1. 远程ssh登陆时报错:/bin/bash: Permission denied

    远程普通用户ssh登录时,提示/bin/bash: Permission denied,用户名mas,密码正确. 首先上个图,用户远程登录步骤,转自http://www.tldp.org/LDP/LG ...

  2. 【解决】org.apache.hadoop.util.Shell$ExitCodeException: /bin/bash: line 0: fg: no job control

    [环境信息] Hadoop版本:2.4.0 客户端OS:Windows Server 2008 R2 服务器端OS:CentOS 6.4 [问题现象] 在通过Windows客户端向Linux服务器提交 ...

  3. CentOS6 Shell脚本/bin/bash^M: bad interpreter错误解决方法

    在windows下保存了一个脚本文件,用ssh上传到centos,添加权限执行nginx提示没有那个文件或目录.shell脚本放到/etc/init.d/目录下,再执行/etc/init.d/ngin ...

  4. 解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题

    PS背景:我在公司做sdk 的pc端开发,所以经常会在win下编译通过之后跑到linux下再运行一次已确保能支持多平台. 今儿在win下跑完一程序,然后放到linux下跑的时候,我用指令: [plai ...

  5. 使用docker exec 就可以进入container,例如:docker exec -it <container_id> /bin/bash

    使用docker exec 就可以进入container,例如:docker exec -it <container_id> /bin/bash

  6. pod install出现[!] /bin/bash -c错误,Installing Realm报错

    pod install出现错误,具体错误信息如下: Installing Realm () [!] /bin/bash -c set -e sh build.sh cocoapods-setup co ...

  7. bin/bash 和 /bin/sh 的区别

    今天在用ssh Secure shell 连接虚拟机中的Ubuntu编写程序时,想比对一下两个源代码有什么差别,但是在一个ssh 客户端下不断的切换很是费劲.于是想着在主机中再添加一个用户.我原本用s ...

  8. Linux:/bin/bash和/bin/sh的区别

    bash.dash(/bin/bash和/bin/sh) 原文:http://www.cnblogs.com/dkblog/archive/2011/04/02/2003822.html Linux中 ...

  9. 执行脚本出现bin/bash: bad interpreter: No such file or directory

    -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory VI打开文件,没发现任何问题, 把/bin/bash ...

随机推荐

  1. 2016 版 Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】

    本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文基于 Laravel 5.2 版本,无奈 5 ...

  2. 原型图利器 – Mockplus的审阅功能

    Mockplus是一款简洁快速的原型图工具 (http://www.mockplus.cn),最近推出了审阅功能. 审阅,旨在解决团队项目原型设计中的沟通和协作的问题. 没有孤立的原型,更没有一次成型 ...

  3. java.lang.IllegalStateException: getWriter() has already been called for this response问题解决

    java.lang.IllegalStateException: getWriter() has already been called for this response问题解决 java.lang ...

  4. Java-try-catch-finally

    try-catch语句还可以包括第三部分,就是finally子句.它表示无论是否出现异常,都应当执行的内容.try-catch-finally语句的一般语法形式为: try { // 可能会发生异常的 ...

  5. Cocos2d-X3.0 刨根问底(六)----- 调度器Scheduler类源码分析

    上一章,我们分析Node类的源码,在Node类里面耦合了一个 Scheduler 类的对象,这章我们就来剖析Cocos2d-x的调度器 Scheduler 类的源码,从源码中去了解它的实现与应用方法. ...

  6. 克隆选择算法-python实现

    CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...

  7. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  8. TCP和Http的区别

    相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可 ...

  9. ExtJS入门教程02,form也可以很优雅

    在上一篇<Extjs window 入门>中,我们已经看到了如何将一个form组件放到window中,今天我们来看看form的一些优雅的工作方式. 使用fieldDefaults,优雅的设 ...

  10. Linux下安装php screw

    1.下载地址:http://sourceforge.net/projects/php-screw/ 2. tar zxvf php_screw_1.5.tar.gz cd php_screw_1.5 ...