为什么/ bin / sh的表现不同为/ bin / bash的,即使一个点到另一个? [英] Why does /bin/sh behave differently to /bin/bash even if one points to the other?

查看:132
本文介绍了为什么/ bin / sh的表现不同为/ bin / bash的,即使一个点到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我是玩弄我的壳调查的答案,<一个href=\"http://stackoverflow.com/questions/26707034/redirect-stderr-through-grep-v-in-lsf-batch-job\">this问题,我注意到,尽管 / bin / sh的指着 /斌/庆典上我的系统中,这两个命令表现不同。首先,输出

While I was playing around in my shell investigating the answer to this question, I noticed that, even though /bin/sh was pointing to /bin/bash on my system, the two commands behave differently. First of all, the output of

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 -c "script.sh 2> >( grep -v FILTER 2>&1 )"

执行成功,这里是输出:

executes successfully, here is the output:

This should be on stderr

有关参考,在这里是内容的 script.sh

For reference, here is the contents of 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

为什么两个调用不同的表现?

Why do the two invocations behave differently?

推荐答案

庆典着眼于价值 $ ARGV [0] (bash是以C实现),以确定它是如何被调用。

bash looks at the value of $argv[0] (bash is implemented in C) to determine how it was invoked.

其行为时,如 SH 调用被记录的在手动

Its behavior when invoked as sh is documented in the manual:

如果bash以名称 SH 启动,它试图模仿启动
   SH 的历史版本尽可能密切的行为,而
  符合POSIX标准。

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.

当作为一个交互式登录shell调用,或者作为非交互式
  与 -login 选项外壳,它首先尝试读取并执行
  从命令 / etc / profile文件〜/ .profile文件,按照这个顺序。该
   - NOPROFILE 选项可用于抑制这种行为。当使用名称 SH 交互的shell的时候,bash寻找变量
   ENV ,扩展它的价值,如果它被定义,并采用扩大价值
  作为一个文件的名称来读取和执行。由于外壳调用为 SH
  并不试图读取并从任何其他启动执行命令
  文件中, - rcfile 选项不起作用。非交互的shell
  这一名称调用 SH 不尝试读取任何其他启动
  文件。

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.

在为 SH 的时候,bash进入POSIX模式后的启动文件
  读

When invoked as sh, Bash enters POSIX mode after the startup files are read

有很长的东西,改变名单(目前46项),当庆典是POSIX模式的 /software/bash/manual/html_node/Bash-POSIX-Mode.html\">documented。

There's a long list (currently 46 items) of things that change when bash is in POSIX mode, documented here.

(POSIX模式可能是有用的大多是作为一种方法来测试移植到非脚本 - 庆典弹)

(POSIX mode is probably useful mostly as a way to test scripts for portability to non-bash shells.)

顺便说一句,这取决于他们在调用该名称改变他们的行为程序是相当普遍的。 的grep fgrep一样的一些版本,而 egrep的实施作为一个可执行文件(尽管GNU 的grep 并没有这样做)。 视图通常是一个符号链接 VIM ;调用它作为视图引起只读模式打开。该 Busybox的系统包括一个数字,都是符号链接到主个人的命令 busybox的可执行文件。

Incidentally, programs that change their behavior depending on the name under which they were invoked are fairly common. Some versions of grep, fgrep, and egrep are implemented as a single executable (though GNU grep doesn't do this). view is typically a symbolic link to vi or vim; invoking it as view causes to open in read-only mode. The Busybox system includes a number of individual commands that are all symlinks to the master busybox executable.

这篇关于为什么/ bin / sh的表现不同为/ bin / bash的,即使一个点到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆