#!/bin/bash

#!/bin/bash: 是指此脚本使用/bin/bash来解释执行。其中,#!是一个特殊的表示符,后面紧跟着解释此脚本的shell路径。bash只是shell的一种,还有很多其它shell,比如:sh,csh,ksh,tcsh等等。

示例演示:
1. #!/bin/bash只能放在第一行,如果后面还有#!,那么只能看成是注释。
这里有三个脚本(脚本都要使用”chmod +x  your scriptname“命令来获得可执行权限):
mybash_1.sh:
#!/bin/sh
source abc
echo "hello abc"

mybash_2.sh:
#!/bin/bash
source abc
echo "hello abc"

mybash_3.sh:
source abc
echo "hello abc"

执行结果:
[root@localhost winine]$ ./ mybash_1.sh  
./mybash_1.sh: line 2: abc: No such file or directory
注:当source命令执行有问题时,sh不再往下面执行。

[ root@localhost winine]$ ./mybash_2.sh 
./mybash_2.sh: line 2: abc: No such file or directory
hello abc
注:当source命令执行有问题时,bash继续执行下面命令。

[ root@localhost winine]$ ./mybash_3.sh 
./mybash_3.sh: line 1: abc: No such file or directory
hello abc
注:自身登录系统所在的shell是bash。所以,当source命令执行有问题时,bash继续执行下面命令。

如果将 mybash_1.sh改成:
echo "abc"
#!/bin/sh
source abc
echo "hello abc"
那么,执行结果是:
[root@localhost winine]$ ./ mybash_1.sh 
abc
./mybash_1.sh: line 3: abc: No such file or directory
hello abc
注:脚本忽略了第二行“#!/bin/sh",直接使用当前所在的shell(也就是bash)来解释脚本。

如果将 mybash_1.sh 改成:
#!/bin/sh
#!/bin/bash
source abc
echo "hello abc"
执行结果为:
[root@localhost winine]$   ./ mybash_1.sh
./mybash_1.sh: line 3: abc: No such file or directory
注:当执行完source命令时,并没有往下执行。说明,#!/bin/sh这一行起到作用了,但#!/bin/bash并没有起作用。在脚本中,不在第一行的#!/bin/bash,只是一个注释。

2. #!后面的路径一定要正确,不正确会报错。
如果我们把 mybash_1.sh中第一行的#!后面加了一个不存在的路径”/home/sh“:
#!/home/sh
source abc
echo "hello abc"
执行结果为:
[root@localhost winine]$   ./ mybash_1.sh
-bash: ./tbash1.sh: /home/sh: bad interpreter: No such file or directory
注:系统会提示/home/sh的路径不存在。

3. 如果在某脚本的第一行没有加上”#!+shell路径“这一行,那么,该脚本会默认使用当前用户登录的shell,作为脚本解释器。(这里不做演示)

  • 13
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值