龙空技术网

浅谈Linux中脚本 " !/bin/bash“和” !/bin/sh"的区别

双主双机热备 5879

前言:

此时兄弟们对“shell和c语言输入的区别”大致比较关注,大家都想要剖析一些“shell和c语言输入的区别”的相关内容。那么小编同时在网上收集了一些对于“shell和c语言输入的区别””的相关资讯,希望咱们能喜欢,各位老铁们一起来了解一下吧!

实在抱歉,由于头条标题限制#的出现,所以在这儿声明一下。

Linux中#!/bin/bash和#!/bin/sh的区别

1、

#!/bin/sh:

指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面跟的是此解释此脚本的shell的路径。

#!/bin/bash:

指此脚本使用/bin/bash来解释执行,#!是特殊的表示符,其后面跟的是此解释此脚本的shell的路径。

补充:

系统支持的shell格式

2、

man sh

man bash

执行man bash、man sh解释是完全一样的。

官方解释如下:

DESCRIPTION

Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).

Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default.

Google翻译如下:

Bash是一种兼容sh的命令语言解释器,它执行从标准输入或文件读取的命令。Bash还包含了Korn和C shell(ksh和csh)的有用功能。

Bash旨在成为IEEE POSIX规范(IEEE标准1003.1)的Shell和Utilities部分的一致实现。 默认情况下,Bash可以配置为符合POSIX标准。

3、

从一个案例感受一下两者的区别

3.1、

新建一个demo.sh脚本

写入

#!/bin/sh

source nonExistent.sh #nonExistent.sh实际并不存在

echo "hello,demo.sh"

3.2、

执行./demo.sh

提示没有文件

即:

source不成功,不会运行source后面的代码

3.3、

修改脚本--改为bash解析

#!/bin/bash

source nonExistent.sh #nonExistent.sh实际并不存在

echo "hello,demo.sh"

3.4、

执行./demo.sh

提示文件错误

但是执行了下一句的echo "hello,demo.sh"

3.5、

指定sh 打开demo.sh

提示文件错误

无打印

解释:

1、sh一般设成bash的软链接

2、 在linux系统当中,使用sh调用执行脚本相当于打开了bash的POSIX标准模式。

即:

/bin/sh 相当于 /bin/bash --posix

那么,sh跟bash的区别,实际上就是bash有没有开启posix模式的区别。

3、

我们把/bin/sh改成/bin/bash --posix测试一下

#!/bin/bash --posix

source nonExistent.sh #nonExistent.sh实际并不存在

echo "hello,demo.sh"

./demo.sh

和#!/bin/sh一样

测试完成

欢迎大家给予宝贵的意见或者建议。

欢迎大家补充或者共享一些其他的方法。

感谢支持。

标签: #shell和c语言输入的区别