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
(or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88
, Dash, ...). Bash can also be considered an implementation of sh
(see below).
Because sh
is a specification, not an implementation, /bin/sh
is a symlink (or a hard link) to an actual implementation on most POSIX systems.
Bash started as an sh
-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 Bash is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.
Bash supports a --posix
switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as sh
.
For a long time, /bin/sh
used to point to /bin/bash
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.
Some popular examples of systems where /bin/sh
does not point to /bin/bash
(and on some of which /bin/bash
may not even exist) are:
sh
to dash
by default;initramfs
. It uses the ash shell implementation.pdksh
, a descendant of the KornShell. FreeBSD's sh
is a descendant of the original Unix Bourne shell. Solaris has its own sh
which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.How can you find out what /bin/sh
points to on your system?
The complication is that /bin/sh
could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:
% file -h /bin/sh
/bin/sh: symbolic link to bash
If it's a hard link, try
% find -L /bin -samefile /bin/sh
/bin/sh
/bin/bash
In fact, the -L
flag covers both symlinks and hardlinks,
but the disadvantage of this method is that it is not portable —
POSIX does not require find
to support the -samefile
option, although both GNU find and FreeBSD find support it.
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line as the very first line of the script.
E.g.
#!/bin/sh
will use sh
(and whatever that happens to point to),
#!/bin/bash
will use /bin/bash
if it's available (and fail with an error message if it's not). Of course, you can also specify another implementation, e.g.
#!/bin/dash
For my own scripts, I prefer sh
for the following reasons:
bash
, they are required to have sh
There are advantages to using bash
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 sh
is a very minimalistic programming language.
Answered 2023-09-21 08:10:34
bash
the display way more useful error messages in the case of syntax error. You can simply save time by using bash. - anyone %
mean at the beginning of your command lines? - anyone $
instead of %
, or #
for the root shell. - anyone %
is commonly the prompt for user shells of the C Shell variety (e.g. csh, tcsh). #
has been traditionally reserved as the prompt character for superuser (root) shells, regardless of which one is chosen. But that's all in the realm of common / typical usage, as historically / traditionally observed. You can use what you like and/or what your users will tolerate. :) Google on how - anyone %
, even though traditionally that was used to signify a Csh-family shell. This is by no means the only area where Zsh deviates from tradition. - anyone sh
: http://man.cx/sh
Bash: http://man.cx/bash
TL;DR: Bash is a superset of sh
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
is Bash. Check sh --version
.
Answered 2023-09-21 08:10:34
sh
in the PATH before the first noncompliant one, that's their fault and they deserve what they get; #!/usr/bin/env sh
and blaming-the-user comes close enough for practical purposes. :) - anyone sh
runs bash
in POSIX mode (gnu.org/software/bash/manual/html_node/…) - anyone 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 as bash
. Here's a quick rundown of common misunderstandings and pitfalls.
First off, you should understand what to expect.
sh scriptname
, or run it with scriptname
and have #!/bin/sh
in the shebang line, you should expect POSIX sh
behavior.bash scriptname
, or run it with scriptname
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?
Bash aims to be backwards-compatible with the Bourne shell and POSIX, but has many additional features. The Bash Reference manual has a section which attempts to enumerate the differences but some common sources of confusion include
[[
is not available in sh
(only [
which is more clunky and limited). See also Difference between single and double square brackets in Bashsh
does not have arrays.local
, source
, function
, shopt
, let
, declare
, and select
are not portable to sh
. (Some sh
implementations support e.g. local
.)for((i=0;i<=3;i++))
loop, +=
increment assignment, etc. The $'string\nwith\tC\aescapes'
feature is tentatively accepted for POSIX (meaning it works in Bash now, but will not yet be supported by sh
on systems which only adhere to the current POSIX specification, and likely will not for some time to come).<<<'here strings'
.*.{png,jpg}
and {0..12}
brace expansion.**
(globstar
) for recursing subdirectories, and extglob
for using a different, more versatile wildcard syntax.~
refers to $HOME
only in Bash (and more generally ~username
to the home directory of username
)./bin/sh
implementations.<(cmd)
and >(cmd)
.&|
for 2>&1 |
and &>
for > ... 2>&1
<>
redirection.${substring:1:2}
, ${variable/pattern/replacement}
, case conversion, etc.$[expression]
syntax which however should be replaced with POSIX arithmetic $((expression))
syntax. (Some legacy pre-POSIX sh
implementations may not support that, though.)type -a
, printf -v
, and the perennial echo -e
.$RANDOM
, $_
, $SECONDS
, $PIPESTATUS[@]
and $FUNCNAME
are Bash extensions. See the Reference Manual./dev/stdin
, /dev/fd/<number>
, /dev/tcp/<network address>
, etcexport variable=value
and[ "x" == "y" ]
which are not portable (export variable
should be separate from variable assignment, and[ ... ]
uses a single equals sign).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 using sh 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. (The shebang line is syntactically a comment, so it is simply ignored in this scenario.)
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 as sh
is not a good way to check if your script is properly portable to ash
/dash
/POSIX sh
or variants like Heirloom sh
.
If you want to check for strict POSIX compliance, try posh
in its designated POSIX mode
(which however does not seem to be properly documented).
As an aside, the POSIX standardization effort is intended to specify the behavior of various U*x-like platform behaviors, including the shell (sh
).
However, this is an evolving document, and so, some implementations adhere to an earlier version of the POSIX specification; furthermore, there are some legacy implementations which didn't even try to adhere to POSIX.
The original Bourne shell had some quirks which were later straightened out by the POSIX spec, which in large parts is based on ksh88
. (Many of the Bash extensions are also innovations from ksh
.)
Answered 2023-09-21 08:10:34
export variable=value
is mandated by POSIX: pubs.opengroup.org/onlinepubs/009695399/utilities/export.html. Perhaps it's not available in some ancient shells, but it's definitely not a bashism. - anyone busybox sh
when working in rescue mode... - anyone 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:
Answered 2023-09-21 08:10:34
sh
(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality). - anyone sh
, then that should be listed as the underying syntax for ksh
too. Conversely, you could argue that the underlying syntax for Bash is ksh
, as Bash has borrowed quite liberally from the Korn shell over the years. - anyone 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:
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
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
Answered 2023-09-21 08:10:34
TERMINAL
SHELL
SH Vs. BASH
SH
BASH
REFERENCE MATERIAL:
SHELL gnu.org:
At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.
A Unix shell is both a command interpreter and a programming language. As a command interpreter, the shell provides the user interface to the rich set of GNU utilities. The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories such as /bin, allowing users or groups to establish custom environments to automate their common tasks.
Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.
A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. The redirection constructs permit fine-grained control of the input and output of those commands. Moreover, the shell allows control over the contents of commands’ environments.
Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cd, break, continue, and exec cannot be implemented outside of the shell because they directly manipulate the shell itself. The history, getopts, kill, or pwd builtins, among others, could be implemented in separate utilities, but they are more convenient to use as builtin commands. All of the shell builtins are described in subsequent sections.
While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.
Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases. Each of these features is described in this manual.
BASH gnu.org:
Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ‘Bourne-Again SHell’, a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix.
Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1). It offers functional improvements over sh for both interactive and programming use.
While the GNU operating system provides other shells, including a version of csh, Bash is the default shell. Like other GNU software, Bash is quite portable. It currently runs on nearly every version of Unix and a few other operating systems - independently-supported ports exist for MS-DOS, OS/2, and Windows platforms.
Answered 2023-09-21 08:10:34
Other answers generally pointed out the difference between Bash and a POSIX shell standard. However, when writing portable shell scripts and being used to Bash syntax, a list of typical bashisms and corresponding pure POSIX solutions is very handy. Such list has been compiled when Ubuntu switched from Bash to Dash as default system shell and can be found here: https://wiki.ubuntu.com/DashAsBinSh
Moreover, there is a great tool called checkbashisms that checks for bashisms in your script and comes handy when you want to make sure that your script is portable.
Answered 2023-09-21 08:10:34
They're nearly identical but bash
has more features – sh
is (more or less) an older subset of bash
.
sh
often means the original Bourne shell
, which predates bash
(Bourne *again* shell
), and was created in 1977. But, in practice, it may be better to think of it as a highly-cross-compatible shell compliant with the POSIX standard from 1992.
Scripts that start with #!/bin/sh
or use the sh
shell usually do so for backwards compatibility. Any unix/linux OS will have an sh
shell. On Ubuntu sh
often invokes dash
and on MacOS it's a special POSIX version of bash
. These shells may be preferred for standard-compliant behavior, speed or backwards compatibility.
bash
is newer than the original sh
, adds more features, and seeks to be backwards compatible with sh
. sh
programs will usually run just fine in bash
. bash
is available on nearly all linux/unix machines and usually used by default – with the notable exception of MacOS defaulting to zsh
as of Catalina (10.15). FreeBSD, by default, does not come with bash
installed.
Answered 2023-09-21 08:10:34
sh
far predates POSIX. These days, you would hope that any sh
you find is at least POSIX-compatible; but on legacy systems this is by no means a given. POSIX stadardizes far more than the shell; in fact, you could argue that the standardization of operating system calls and library functions is more important. - anyone sh
programs should run fine in Bash in practice, not just "in theory". There are obviously corner cases like when a script uses a variable whose name is reserved by Bash but not by other shells. - anyone /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. Type info bash
and read the "Bash Features" section (section 6 in the current version), or read the current documentation online.
Answered 2023-09-21 08:10:34
sh
only gives you a POSIX shell, if you have the right PATH
set up in your current shell. There is no defined PATH-name that gives you a POSIX shell. - anyone sh
was not necessarily even giving you a POSIX shell, on Solaris, for example. - anyone The differences explained in the easiest way possible:
After having a basic understanding, the other answers will be easier to understand.
Shell - "Shell" is a program, which facilitates the interaction between the user and the operating system (kernel). There are many shell implementations available, like sh, Bash, C shell, Z shell, etc.
Using any of the shell programs, we will be able to execute commands that are supported by that shell program.
Bash - It derived from Bourne-again Shell. Using this program, we will be able to execute all the commands specified by the shell. Also, we will be able to execute some commands that are specifically added to this program. Bash has backward compatibility with sh.
Sh - It derived from Bourne Shell. "sh" supports all the commands specified in the shell. It means, using this program, we will be able to execute all the commands specified by Shell.
For more information, see:
Answered 2023-09-21 08:10:34
The Linux operating system offers different types of shell. Though shells have many commands in common, each type has unique features. Let’s study different kind of mostly used shells.
Sh shell:
Sh shell is also known as Bourne shell. Sh shell is the first shell developed for Unix computers by Stephen Bourne at AT&T's Bell Labs in 1977. It includes many scripting tools.
Bash shell:
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most Linux distributions and substitute for the Sh shell (the Sh shell will also run in the Bash shell). The Bash shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
Answered 2023-09-21 08:10:34