Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>What is sh</h1> <p><code>sh</code> (or the Shell Command Language) is a programming language described by the <a href="http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html" rel="noreferrer">POSIX standard</a>. It has many implementations (<code>ksh88</code>, <code>dash</code>, ...). <code>bash</code> can also be considered an implementation of <code>sh</code> (see below).</p> <p>Because <code>sh</code> is a specification, not an implementation, <code>/bin/sh</code> is a symlink (or a hard link) to an actual implementation on most POSIX systems.</p> <h1>What is bash</h1> <p><code>bash</code> started as an <code>sh</code>-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself <code>bash</code> is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.</p> <p><code>bash</code> supports a <code>--posix</code> switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as <code>sh</code>.</p> <h1>sh = bash?</h1> <p>For a long time, <code>/bin/sh</code> used to point to <code>/bin/bash</code> on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.</p> <p>Some popular examples of systems where <code>/bin/sh</code> does not point to <code>/bin/bash</code> (and on some of which <code>/bin/bash</code> may not even exist) are:</p> <ol> <li>Modern Debian and Ubuntu systems, which symlink <code>sh</code> to <code>dash</code> by default;</li> <li><a href="https://en.wikipedia.org/wiki/BusyBox" rel="noreferrer">Busybox</a>, which is usually run during the Linux system boot time as part of <code>initramfs</code>. It uses the <code>ash</code> shell implementation.</li> <li>BSDs, and in general any non-Linux systems. OpenBSD uses <code>pdksh</code>, a descendant of the Korn shell. FreeBSD's <code>sh</code> is a descendant of the original UNIX Bourne shell. Solaris has its own <code>sh</code> which for a long time was not POSIX-compliant; a free implementation is available from the <a href="http://heirloom.sourceforge.net/sh.html" rel="noreferrer">Heirloom project</a>.</li> </ol> <p>How can you find out what <code>/bin/sh</code> points to on your system?</p> <p>The complication is that <code>/bin/sh</code> could be a symbolic link or a hard link. If it's a symbolic link, a <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html" rel="noreferrer">portable</a> way to resolve it is:</p> <pre><code>% file -h /bin/sh /bin/sh: symbolic link to bash </code></pre> <p>If it's a hard link, try</p> <pre><code>% find -L /bin -samefile /bin/sh /bin/sh /bin/bash </code></pre> <p>In fact, the <code>-L</code> flag covers both symlinks and hardlinks, but the disadvantage of this method is that it is not portable — POSIX <a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html" rel="noreferrer">does not require</a> <code>find</code> to support the <code>-samefile</code> option, although both <a href="https://www.gnu.org/software/findutils/manual/html_mono/find.html#Hard-Links" rel="noreferrer">GNU find</a> and <a href="https://www.freebsd.org/cgi/man.cgi?find(1)" rel="noreferrer">FreeBSD find</a> support it.</p> <h1>Shebang line</h1> <p>Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.</p> <p>E.g.</p> <pre><code>#!/bin/sh </code></pre> <p>will use <code>sh</code> (and whatever that happens to point to),</p> <pre><code>#!/bin/bash </code></pre> <p>will use <code>/bin/bash</code> if it's available (and fail with an error message if it's not). Of course, you can also specify another implementation, e.g.</p> <pre><code>#!/bin/dash </code></pre> <h1>Which one to use</h1> <p>For my own scripts, I prefer <code>sh</code> for the following reasons:</p> <ul> <li>it is standardized</li> <li>it is much simpler and easier to learn</li> <li>it is portable across POSIX systems — even if they happen not to have <code>bash</code>, they are required to have <code>sh</code></li> </ul> <p>There are advantages to using <code>bash</code> as well. Its features make programming more convenient and similar to programming in other modern programming languages. These include things like scoped local variables and arrays. Plain <code>sh</code> is a very minimalistic programming language.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload