When writing shell programs, we often use /bin/sh
and /bin/bash
. I usually use bash
, but I don't know what's the difference between them.
What's main difference between bash
and sh
?
What do we need to be aware of when programming in bash
and sh
?
sh
: http://man.cx/shbash
: http://man.cx/bashTL;DR:
bash
is a superset ofsh
with a more elegant syntax and more functionality. It is safe to use a bash shebang line in almost all cases as it's quite ubiquitous on modern platforms.NB: in some environments,
sh
isbash
. Checksh --version
.This question has frequently been nominated as a canonical for people who try to use
sh
and are surprised that it's not behaving the same asbash
. Here's a quick rundown of common misunderstandings and pitfalls.First off, you should understand what to expect.
sh scriptname
, or run it withscriptname
and have#!/bin/sh
in the shebang line, you should expect POSIXsh
behavior.bash scriptname
, or run it withscriptname
and have#!/bin/bash
(or the local equivalent) in the shebang line, you should expect Bash behavior.Having a correct shebang and running the script by typing just the script name (possibly with a relative or full path) is generally the preferred solution. In addition to a correct shebang, this requires the script file to have execute permission (
chmod a+x scriptname
).So, how do they actually differ?
The Bash Reference manual has a section which attempts to enumerate the differences but some common sources of confusion include
[[
is not available insh
(only[
which is more clunky and limited).sh
does not have arrays.local
,function
, andselect
are not portable tosh
.$'string\nwith\tC\aescapes'
and the three-argumentfor((i=0;i<=3;i++))
loop,+=
increment assignment, etc.<<<'here strings'
.*.{png,jpg}
and{0..9}
brace expansion.This is in POSIX, but may be mising from some pre-POSIX~
refers to$HOME
only in Bash (and more generally~username
to the home directory ofusername
)./bin/sh
implementations.<(cmd)
and>(cmd)
.<>
redirection.${substring:1:2}
,${variable/pattern/replacement}
, case conversion, etc.Remember, this is an abridged listing. Refer to the reference manual for the full scoop, and http://mywiki.wooledge.org/Bashism for many good workarounds; and/or try http://shellcheck.net/ which warns for many Bash-only features.
A common error is to have a
#!/bin/bash
shebang line, but then nevertheless usingsh scriptname
to actually run the script. This basically disables any Bash-only functionality, so you get syntax errors e.g. for trying to use arrays.Unfortunately, Bash will not warn when you try to use these constructs when it is invoked as
sh
. It doesn't completely disable all Bash-only functionality, either, so running Bash by invoking it assh
is not a good way to check if your script is properly portable toash
/dash
/POSIXsh
or variants like Heirloomsh
Shell is an interface between a user and OS to access to an operating system's services. It can be either GUI or CLI (Command Line interface).
sh (Bourne shell) is a shell command-line interpreter, for Unix/Unix-like operating systems. It provides some built-in commands. In scripting language we denote interpreter as
#!/bin/sh
. It was one most widely supported by other shells like bash (free/open), kash (not free).Bash (Bourne again shell) is a shell replacement for the Bourne shell. Bash is superset of sh. Bash supports sh. POSIX is a set of standards defining how POSIX-compliant systems should work. Bash is not actually a POSIX compliant shell. In a scripting language we denote the interpreter as
#!/bin/bash
.Analogy:
TERMINAL
SHELL
SH Vs. BASH
SH
BASH
REFERENCE MATERIAL:
SHELL gnu.org:
BASH gnu.org:
Post from UNIX.COM
Shell features
This table below lists most features that I think would make you choose one shell over another. It is not intended to be a definitive list and does not include every single possible feature for every single possible shell. A feature is only considered to be in a shell if in the version that comes with the operating system, or if it is available as compiled directly from the standard distribution. In particular the C shell specified below is that available on SUNOS 4.*, a considerable number of vendors now ship either tcsh or their own enhanced C shell instead (they don't always make it obvious that they are shipping tcsh.
Code:
Key to the table above.
Y Feature can be done using this shell.
N Feature is not present in the shell.
F Feature can only be done by using the shells function mechanism.
L The readline library must be linked into the shell to enable this Feature.
Notes to the table above
/bin/sh
may or may not invoke the same program as/bin/bash
.sh
supports at least the features required by POSIX (assuming a correct implementation). It may support extensions as well.bash
, the "Bourne Again Shell", implements the features required for sh plus bash-specific extensions. The full set of extensions is too long to describe here, and it varies with new releases. The differences are documented in the bash manual. Typeinfo bash
and read the "Bash Features" section (section 6 in the current version), or read the current documentation online.