linux bin sh bin bash,关于linux:为什么/ bin / sh与/ bin / bash的行为不同,即使一个指向另一个?...

当我在我的shell中调查这个问题的答案时,我注意到,即使/bin/sh指向我系统上的/bin/bash,这两个命令的行为也不同。 首先,输出

ls -lh /bin/sh

是:

lrwxrwxrwx 1 root root 4 Apr 22  2013 /bin/sh -> bash*

但是,通过/bin/sh调用以下命令:

/bin/sh -c"script.sh 2> >( grep -v FILTER 2>&1 )"

返回此错误:

/bin/sh: -c: line 0: syntax error near unexpected token '>'

/bin/sh: -c: line 0: 'script.sh 2> >( grep -v FILTER 2>&1 )'

通过/bin/bash运行相同的命令时:

/bin/bash -c"script.sh 2> >( grep -v FILTER 2>&1 )"

执行成功,这是输出:

This should be on stderr

作为参考,这是script.sh的内容:

#!/bin/sh

echo"FILTER: This should be filtered out" 1>&2

echo"This should be on stderr" 1>&2

echo"FILTER: This should be filtered out" 1>&2

为什么两个调用的行为不同?

通常情况下,同一个人会根据您的处理方式做出不同的反应。

stackoverflow.com/q/20195377/834309

bash查看$argv[0]的值(bash在C中实现)以确定它是如何被调用的。

调用sh时的行为记录在手册中:

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 as a non-interactive

shell with the -login option, it first attempts to read and execute

commands from /etc/profile 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

当bash处于POSIX模式时,会有很长的列表(当前有46项)会发生变化,这里记录了这些内容。

(POSIX模式可能主要用作测试脚本以便非可移植到非bash shell的方法。)

顺便提一下,根据调用它们的名称改变其行为的程序是相当普遍的。某些版本的grep,fgrep和egrep实现为单个可执行文件(尽管GNU grep不执行此操作)。 view通常是vi或vim的符号链接;调用它作为view导致以只读模式打开。 Busybox系统包含许多单独的命令,这些命令都是主busybox可执行文件的符号链接。

谢谢! 这真是太棒了,我以前从未在手册中注意到这一点。

这也是busybox工具确定要做什么的方式。 在内部查看$ argv [0]是一种很好的方法,可以使一个二进制表现为几个单独的工具。

@ pierce.jason:在某些实现中,grep,fgrep和egrep以这种方式工作。 (我刚刚发现GNU grep没有这样做;它们是三个独立的可执行文件。)同样,view通常与vi或vim相同的可执行文件。 以view调用它相当于使用-R(readonly)选项。

view is typically a symbolic link to vi or vim并且在许多系统中,vi实际上是在vi兼容模式下运行的vim。

调用bash作为sh会导致它在读取通常读取的启动文件后进入posix模式(与POSIX sh读取的启动文件相反).Bash有许多不同的调用模式。您可以从本手册的INVOCATION部分了解这些模式。以下是有关POSIX模式的一些细节。

POSIX模式

这种模式意味着bash将在不同程度上尝试符合POSIX期望。正如这里所解释的那样,bash对这种模式有几种不同的调用,其含义略有不同:

sh:Bash在读取启动文件后进入POSIX模式。

bash --posix:Bash在读取启动文件之前进入POSIX模式。

set -o posix:Bash切换到POSIX模式。

POSIXLY_CORRECT:如果此变量在bash启动时处于环境中,则shell会在读取启动文件之前进入posix模式,如bash --posix。如果在bash运行时设置,如set -o posix。

从Bash参考手册:

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.

因为bash二进制文件检查它是如何被调用的(通过argv[0])并且如果它作为sh运行则进入兼容模式。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值