您的位置:首页 > 运维架构 > Shell

#!/bin/bash

2016-01-14 11:09 736 查看
#!/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继续执行下面命令。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux bash