bash和sh的区别

问题:我们通常不会赋予bash脚本可执行权限,需要使用的时候使用 sh <脚本名>来执行。因为从可执行程序看,sh只是bash的一个link,因此我们通常认为这两者是等价。

[root@huayd1 huayd]# which sh
/usr/bin/sh
[root@huayd1 huayd]# ls -l /usr/bin/sh
lrwxrwxrwx. 1 root root 4 Feb 23  2017 /usr/bin/sh -> bash

但是,实则不然。以下部分摘自bash的man page。

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/pro†file and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

When bash is started in posix mode, as with the --posix command line option, it follows the POSIX standard for startup files. In this mode, interactive shells expand the ENV variable and commands are read and executed from the file whose name is the expanded value. No other startup files are read.

[root@huayd1 huayd]# cat t1.sh 
#!/bin/bash

source ~/.bash_profile

function f1() {
      echo 1
}

function f1-1() {
      echo "f1-1"
}

function f1_1() {
    echo "f1_1"
}  


f1  
f1-1
f1_1
[root@huayd1 huayd]# 
[root@huayd1 huayd]# sh t1.sh 
t1.sh: line 11: `f1-1': not a valid identifier
[root@huayd1 huayd]#  
[root@huayd1 huayd]# bash t1.sh 
1
f1-1
f1_1
[root@huayd1 huayd]# 
[root@huayd1 huayd]# bash --posix t1.sh
t1.sh: line 11: `f1-1': not a valid identifier
[root@huayd1 huayd]# 

结论: sh 等价于 bash --posix

--End
Mason

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 说明,本文所使用的机器是:SUSE Linux Enterprise。 问题定位 这看起来像是环境变量引起的问题,...
    lework阅读 3,067评论 6 11
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,940评论 0 10
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,760评论 0 5
  • 反思,努力工作的目的? 是为了挣钱,还是为了认可,还是为了优越感?
    从心印心阅读 193评论 0 0
  • 情是一味良药,用好了,可以让人怀念一生。用不好,只能一次次的将自己伤害。”的确,不是每一段爱情都会让我们觉得甜蜜非...
    秋舞阅读 268评论 0 0

友情链接更多精彩内容