Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sh和bash之间的区别_Bash_Shell_Unix_Sh - Fatal编程技术网

sh和bash之间的区别

sh和bash之间的区别,bash,shell,unix,sh,Bash,Shell,Unix,Sh,在编写shell程序时,我们经常使用/bin/sh和/bin/bash。我通常使用bash,但我不知道它们之间有什么区别 bash和sh之间的主要区别是什么 在bash和sh中编程时,我们需要注意什么?sh: bash: TL;DR:bash是sh的超集,具有更优雅的语法和更多功能。在几乎所有情况下使用bash-shebang行都是安全的,因为它在现代平台上非常普遍 注意:在某些环境中,sh是bash。检查sh--version什么是sh sh(或Shell命令语言)是由描述的编程语言。 它有许

在编写shell程序时,我们经常使用
/bin/sh
/bin/bash
。我通常使用
bash
,但我不知道它们之间有什么区别

bash
sh
之间的主要区别是什么

bash
sh
中编程时,我们需要注意什么?

sh

bash

TL;DR
bash
sh
的超集,具有更优雅的语法和更多功能。在几乎所有情况下使用bash-shebang行都是安全的,因为它在现代平台上非常普遍

注意:在某些环境中,
sh
bash
。检查
sh--version

什么是sh
sh
(或Shell命令语言)是由描述的编程语言。 它有许多实现(
ksh88
dash
,…)<代码>bash也可以 考虑实施
sh
(见下文)

因为
sh
是一个规范,而不是一个实现,
/bin/sh
是一个符号链接 (或硬链接)到大多数POSIX系统上的实际实现

什么是bash
bash
最初是一个兼容
sh
的实现(虽然它比POSIX标准早几年),但随着时间的推移,它获得了许多扩展。其中许多扩展可能会更改有效POSIX shell脚本的行为,因此本身
bash
不是有效的POSIX shell。相反,它是POSIX shell语言的一种方言

bash
支持一个
--posix
开关,这使得它更符合posix。如果作为
sh
调用,它还会尝试模拟POSIX

sh=bash? 长期以来,在大多数GNU/Linux系统上,
/bin/sh
用于指向
/bin/bash
。因此,忽略两者之间的差异几乎是安全的。但这种情况最近开始改变

/bin/sh
不指向
/bin/bash
(其中一些
/bin/bash
可能不存在)的系统的一些常见示例如下:

  • 现代Debian和Ubuntu系统,默认情况下将
    sh
    符号链接到
    dash
  • ,它通常在Linux系统启动时作为
    initramfs
    的一部分运行。它使用
    ash
    shell实现
  • BSD,以及一般任何非Linux系统。OpenBSD使用KornShell的后代
    pdksh
    。FreeBSD的
    sh
    是原始UNIX Bourne shell的后代。Solaris有自己的
    sh
    ,很长一段时间不符合POSIX;可以从中获得免费的实现
  • 如何找出
    /bin/sh
    指向系统的内容

    复杂的是,
    /bin/sh
    可以是符号链接,也可以是硬链接。 如果是符号链接,解决方法是:

    % file -h /bin/sh
    /bin/sh: symbolic link to bash
    
    如果是硬链接,请尝试

    % find -L /bin -samefile /bin/sh
    /bin/sh
    /bin/bash
    
    事实上,
    -L
    标志包括符号链接和硬链接, 但这种方法的缺点是不可移植- POSIX
    find
    支持
    -samefile
    选项, 尽管双方都支持它

    射邦线 最终,由您决定使用哪一行,将«shebang»行作为脚本的第一行

    例如

    将使用
    sh
    (以及指向的任何内容)

    如果可用,将使用
    /bin/bash
    (如果不可用,将失败并显示错误消息)。当然,您也可以指定另一个实现,例如

    #!/bin/dash
    
    用哪一个 对于我自己的脚本,我更喜欢
    sh
    ,原因如下:

    • 它是标准化的
    • 它更简单,更容易学习
    • 它可以跨POSIX系统移植-即使它们碰巧没有
      bash
      ,它们也必须有
      sh

    使用
    bash也有很多优点。它的特性使编程更加方便,并且与其他现代编程语言的编程类似。其中包括作用域局部变量和数组。Plain
    sh
    是一种非常简约的编程语言。

    Shell是用户和操作系统之间访问操作系统服务的接口。它可以是GUI或CLI(命令行界面)

    sh(Bourneshell)是一个shell命令行解释器,用于Unix/Unix类操作系统。它提供了一些内置命令。在脚本语言中,我们将解释器表示为
    #/bin/sh
    。它是其他shell(如bash(free/open)、kash(notfree)最广泛支持的shell之一

    BashBourneagainshell)是Bourne shell的替代品。Bash是sh的超集。Bash支持sh。POSIX是一组定义符合POSIX的系统应该如何工作的标准。Bash实际上不是一个兼容POSIX的shell。在脚本语言中,我们将解释器表示为
    #/bin/bash

    类比:

    • Shell类似于接口、规范或API
    • sh是一个实现Shell接口的类
    • Bash是sh的一个子类

    /bin/sh
    可以调用也可以不调用与
    /bin/bash
    相同的程序

    sh
    至少支持这些功能(假设实现正确)。它还可以支持扩展

    bash
    “Bourne-reach-Shell”实现了sh-plus-bash特定扩展所需的功能。完整的扩展集太长,无法在这里描述,并且随着新版本的不同而有所不同。这些差异记录在bash手册中。键入
    info bash
    并阅读“bash功能”部分(当前版本中的第6部分
    #!/bin/bash
    
    #!/bin/dash
    
                                         sh   csh  ksh  bash tcsh zsh  rc   es
    Job control                          N    Y    Y    Y    Y    Y    N    N
    Aliases                              N    Y    Y    Y    Y    Y    N    N
    Shell functions                      Y(1) N    Y    Y    N    Y    Y    Y
    "Sensible" Input/Output redirection  Y    N    Y    Y    N    Y    Y    Y
    Directory stack                      N    Y    Y    Y    Y    Y    F    F
    Command history                      N    Y    Y    Y    Y    Y    L    L
    Command line editing                 N    N    Y    Y    Y    Y    L    L
    Vi Command line editing              N    N    Y    Y    Y(3) Y    L    L
    Emacs Command line editing           N    N    Y    Y    Y    Y    L    L
    Rebindable Command line editing      N    N    N    Y    Y    Y    L    L
    User name look up                    N    Y    Y    Y    Y    Y    L    L
    Login/Logout watching                N    N    N    N    Y    Y    F    F
    Filename completion                  N    Y(1) Y    Y    Y    Y    L    L
    Username completion                  N    Y(2) Y    Y    Y    Y    L    L
    Hostname completion                  N    Y(2) Y    Y    Y    Y    L    L
    History completion                   N    N    N    Y    Y    Y    L    L
    Fully programmable Completion        N    N    N    N    Y    Y    N    N
    Mh Mailbox completion                N    N    N    N(4) N(6) N(6) N    N
    Co Processes                         N    N    Y    N    N    Y    N    N
    Builtin artithmetic evaluation       N    Y    Y    Y    Y    Y    N    N
    Can follow symbolic links invisibly  N    N    Y    Y    Y    Y    N    N
    Periodic command execution           N    N    N    N    Y    Y    N    N
    Custom Prompt (easily)               N    N    Y    Y    Y    Y    Y    Y
    Sun Keyboard Hack                    N    N    N    N    N    Y    N    N
    Spelling Correction                  N    N    N    N    Y    Y    N    N
    Process Substitution                 N    N    N    Y(2) N    Y    Y    Y
    Underlying Syntax                    sh   csh  sh   sh   csh  sh   rc   rc
    Freely Available                     N    N    N(5) Y    Y    Y    Y    Y
    Checks Mailbox                       N    Y    Y    Y    Y    Y    F    F
    Tty Sanity Checking                  N    N    N    N    Y    Y    N    N
    Can cope with large argument lists   Y    N    Y    Y    Y    Y    Y    Y
    Has non-interactive startup file     N    Y    Y(7) Y(7) Y    Y    N    N
    Has non-login startup file           N    Y    Y(7) Y    Y    Y    N    N
    Can avoid user startup files         N    Y    N    Y    N    Y    Y    Y
    Can specify startup file             N    N    Y    Y    N    N    N    N
    Low level command redefinition       N    N    N    N    N    N    N    Y
    Has anonymous functions              N    N    N    N    N    N    Y    Y
    List Variables                       N    Y    Y    N    Y    Y    Y    Y
    Full signal trap handling            Y    N    Y    Y    N    Y    Y    Y
    File no clobber ability              N    Y    Y    Y    Y    Y    N    F
    Local variables                      N    N    Y    Y    N    Y    Y    Y
    Lexically scoped variables           N    N    N    N    N    N    N    Y
    Exceptions                           N    N    N    N    N    N    N    Y
    
    1. This feature was not in the original version, but has since become
       almost standard.
    2. This feature is fairly new and so is often not found on many
       versions of the shell, it is gradually making its way into
       standard distribution.
    3. The Vi emulation of this shell is thought by many to be
       incomplete.
    4. This feature is not standard but unofficial patches exist to
       perform this.
    5. A version called 'pdksh' is freely available, but does not have
       the full functionality of the AT&T version.
    6. This can be done via the shells programmable completion mechanism.
    7. Only by specifying a file via the ENV environment variable.