Difference between sh and bash
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?
bash shell unix sh
migrated from programmers.stackexchange.com Apr 20 '11 at 3:54
This question came from our site for professionals, academics, and students working within the systems development life cycle.
add a comment |
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?
bash shell unix sh
migrated from programmers.stackexchange.com Apr 20 '11 at 3:54
This question came from our site for professionals, academics, and students working within the systems development life cycle.
19
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
4
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03
add a comment |
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?
bash shell unix sh
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?
bash shell unix sh
bash shell unix sh
edited Aug 19 '16 at 1:45
Jianxin Gao
1,0501924
1,0501924
asked Apr 20 '11 at 3:33
Weiwei YangWeiwei Yang
5,16131010
5,16131010
migrated from programmers.stackexchange.com Apr 20 '11 at 3:54
This question came from our site for professionals, academics, and students working within the systems development life cycle.
migrated from programmers.stackexchange.com Apr 20 '11 at 3:54
This question came from our site for professionals, academics, and students working within the systems development life cycle.
19
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
4
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03
add a comment |
19
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
4
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03
19
19
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
4
4
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03
add a comment |
9 Answers
9
active
oldest
votes
What is 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.
What is bash
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.
sh = bash?
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:
- Modern Debian and Ubuntu systems, which symlink
shtodashby default;
Busybox, which is usually run during the Linux system boot time as part ofinitramfs. It uses theashshell implementation.- BSDs, and in general any non-Linux systems. OpenBSD uses
pdksh, a descendant of the Korn shell. FreeBSD'sshis a descendant of the original UNIX Bourne shell. Solaris has its ownshwhich 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.
Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
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
Which one to use
For my own scripts, I prefer sh for the following reasons:
- it is standardized
- it is much simpler and easier to learn
- it is portable across POSIX systems — even if they happen not to have
bash, they are required to havesh
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.
13
It probably means that they specify/bin/tcshas the default interactive shell in/etc/passwd, not that they usetcshas anshimplementation.tcshhas the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD'ssh.
– Roman Cheplyaka
Apr 14 '14 at 10:49
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
In practice almost every implementation ofshsupports local variables, declared with the keywordlocal.
– August Karlstrom
Nov 17 '14 at 6:35
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
@IanTait Instead ofSTR =~ REGEXyou can useecho STR | grep -q REGEX.
– August Karlstrom
Feb 10 '17 at 8:46
|
show 12 more comments
sh: http://man.cx/shbash: 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.
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
add a comment |
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
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.
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
|
show 4 more comments
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.
- If you run your script with
sh scriptname, or run it withscriptnameand have#!/bin/shin the shebang line, you should expect POSIXshbehavior. - If you run your script with
bash scriptname, or run it withscriptnameand 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).
shdoes not have arrays.- Some Bash keywords like
local,function, andselectare not portable tosh. - Bash has many C-style syntax extensions like
$'stringnwithtCaescapes'and the three-argumentfor((i=0;i<=3;i++))loop,+=increment assignment, etc. - Bash supports
<<<'here strings'. - Bash has
*.{png,jpg}and{0..9}brace expansion.
This is in POSIX, but may be mising from some pre-POSIX~refers to$HOMEonly in Bash (and more generally~usernameto the home directory ofusername)./bin/shimplementations.- Bash has process substitution with
<(cmd)and>(cmd). - Bash supports coprocesses with
<>redirection. - Bash has significantly extended facilities for shell arithmetic (though still no floating-point support) and variable substring manipulation with
${substring:1:2},${variable/pattern/replacement}, case conversion, etc. - Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
- Many, many convenience features for interactive use which however do not affect script behavior.
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.
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
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
add a comment |
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:
- Shell is like an interface or specifications or API.
- sh is a class which implements the Shell interface.
- Bash is a subclass of the sh.

2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
I think this is trying to say Bash inherits fromsh(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).
– tripleee
May 19 '18 at 4:44
add a comment |
TERMINAL
- program(s) that put a window up
- xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
SHELL
- Is a program that runs in the terminal
- Shell is both a command interpreter and a programming language
- Shell is simply a macro processor that executes commands.
- Macro processor means functionality where text and symbols are expanded to create larger expressions.
SH Vs. BASH
SH
- (SHell)
- Is a specific shell
- a command interpreter and a programming language
- Predecessor of BASH
BASH
- (Bourne-Again SHell)
- Is a specific shell
- a command interpreter and a programming language
- Has sh functionality and more
- Successor of SH
- BASH is the default SHELL
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.
add a comment |
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.
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
add a comment |
/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.
shonly gives you a POSIX shell, if you have the rightPATHset up in your current shell. There is no defined PATH-name that gives you a POSIX shell.
– schily
Sep 12 '15 at 21:37
For a long time,shwas not necessarily even giving you a POSIX shell, on Solaris, for example.
– tripleee
Apr 14 '16 at 4:19
add a comment |
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 include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5725296%2fdifference-between-sh-and-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
What is 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.
What is bash
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.
sh = bash?
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:
- Modern Debian and Ubuntu systems, which symlink
shtodashby default;
Busybox, which is usually run during the Linux system boot time as part ofinitramfs. It uses theashshell implementation.- BSDs, and in general any non-Linux systems. OpenBSD uses
pdksh, a descendant of the Korn shell. FreeBSD'sshis a descendant of the original UNIX Bourne shell. Solaris has its ownshwhich 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.
Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
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
Which one to use
For my own scripts, I prefer sh for the following reasons:
- it is standardized
- it is much simpler and easier to learn
- it is portable across POSIX systems — even if they happen not to have
bash, they are required to havesh
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.
13
It probably means that they specify/bin/tcshas the default interactive shell in/etc/passwd, not that they usetcshas anshimplementation.tcshhas the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD'ssh.
– Roman Cheplyaka
Apr 14 '14 at 10:49
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
In practice almost every implementation ofshsupports local variables, declared with the keywordlocal.
– August Karlstrom
Nov 17 '14 at 6:35
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
@IanTait Instead ofSTR =~ REGEXyou can useecho STR | grep -q REGEX.
– August Karlstrom
Feb 10 '17 at 8:46
|
show 12 more comments
What is 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.
What is bash
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.
sh = bash?
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:
- Modern Debian and Ubuntu systems, which symlink
shtodashby default;
Busybox, which is usually run during the Linux system boot time as part ofinitramfs. It uses theashshell implementation.- BSDs, and in general any non-Linux systems. OpenBSD uses
pdksh, a descendant of the Korn shell. FreeBSD'sshis a descendant of the original UNIX Bourne shell. Solaris has its ownshwhich 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.
Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
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
Which one to use
For my own scripts, I prefer sh for the following reasons:
- it is standardized
- it is much simpler and easier to learn
- it is portable across POSIX systems — even if they happen not to have
bash, they are required to havesh
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.
13
It probably means that they specify/bin/tcshas the default interactive shell in/etc/passwd, not that they usetcshas anshimplementation.tcshhas the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD'ssh.
– Roman Cheplyaka
Apr 14 '14 at 10:49
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
In practice almost every implementation ofshsupports local variables, declared with the keywordlocal.
– August Karlstrom
Nov 17 '14 at 6:35
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
@IanTait Instead ofSTR =~ REGEXyou can useecho STR | grep -q REGEX.
– August Karlstrom
Feb 10 '17 at 8:46
|
show 12 more comments
What is 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.
What is bash
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.
sh = bash?
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:
- Modern Debian and Ubuntu systems, which symlink
shtodashby default;
Busybox, which is usually run during the Linux system boot time as part ofinitramfs. It uses theashshell implementation.- BSDs, and in general any non-Linux systems. OpenBSD uses
pdksh, a descendant of the Korn shell. FreeBSD'sshis a descendant of the original UNIX Bourne shell. Solaris has its ownshwhich 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.
Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
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
Which one to use
For my own scripts, I prefer sh for the following reasons:
- it is standardized
- it is much simpler and easier to learn
- it is portable across POSIX systems — even if they happen not to have
bash, they are required to havesh
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.
What is 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.
What is bash
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.
sh = bash?
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:
- Modern Debian and Ubuntu systems, which symlink
shtodashby default;
Busybox, which is usually run during the Linux system boot time as part ofinitramfs. It uses theashshell implementation.- BSDs, and in general any non-Linux systems. OpenBSD uses
pdksh, a descendant of the Korn shell. FreeBSD'sshis a descendant of the original UNIX Bourne shell. Solaris has its ownshwhich 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.
Shebang line
Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.
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
Which one to use
For my own scripts, I prefer sh for the following reasons:
- it is standardized
- it is much simpler and easier to learn
- it is portable across POSIX systems — even if they happen not to have
bash, they are required to havesh
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.
edited Apr 20 '17 at 7:53
answered Apr 20 '11 at 4:10
Roman CheplyakaRoman Cheplyaka
28.4k456101
28.4k456101
13
It probably means that they specify/bin/tcshas the default interactive shell in/etc/passwd, not that they usetcshas anshimplementation.tcshhas the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD'ssh.
– Roman Cheplyaka
Apr 14 '14 at 10:49
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
In practice almost every implementation ofshsupports local variables, declared with the keywordlocal.
– August Karlstrom
Nov 17 '14 at 6:35
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
@IanTait Instead ofSTR =~ REGEXyou can useecho STR | grep -q REGEX.
– August Karlstrom
Feb 10 '17 at 8:46
|
show 12 more comments
13
It probably means that they specify/bin/tcshas the default interactive shell in/etc/passwd, not that they usetcshas anshimplementation.tcshhas the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD'ssh.
– Roman Cheplyaka
Apr 14 '14 at 10:49
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
In practice almost every implementation ofshsupports local variables, declared with the keywordlocal.
– August Karlstrom
Nov 17 '14 at 6:35
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
@IanTait Instead ofSTR =~ REGEXyou can useecho STR | grep -q REGEX.
– August Karlstrom
Feb 10 '17 at 8:46
13
13
It probably means that they specify
/bin/tcsh as the default interactive shell in /etc/passwd, not that they use tcsh as an sh implementation. tcsh has the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD's sh.– Roman Cheplyaka
Apr 14 '14 at 10:49
It probably means that they specify
/bin/tcsh as the default interactive shell in /etc/passwd, not that they use tcsh as an sh implementation. tcsh has the C shell syntax; it isn't even remotely compatible with the sh POSIX standard. See freebsd.org/cgi/man.cgi?query=sh&sektion=1 for the description of FreeBSD's sh.– Roman Cheplyaka
Apr 14 '14 at 10:49
3
3
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
Thanks for a great explanation! For reference, sh != bash on MacOS as well.
– bizi
Oct 5 '14 at 6:28
3
3
In practice almost every implementation of
sh supports local variables, declared with the keyword local.– August Karlstrom
Nov 17 '14 at 6:35
In practice almost every implementation of
sh supports local variables, declared with the keyword local.– August Karlstrom
Nov 17 '14 at 6:35
3
3
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
@bizi Korays-MacBook-Pro:hello2 koraytugay$ sh --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc. Korays-MacBook-Pro:hello2 koraytugay$ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14) Copyright (C) 2007 Free Software Foundation, Inc.
– Koray Tugay
Apr 11 '15 at 17:36
2
2
@IanTait Instead of
STR =~ REGEX you can use echo STR | grep -q REGEX .– August Karlstrom
Feb 10 '17 at 8:46
@IanTait Instead of
STR =~ REGEX you can use echo STR | grep -q REGEX .– August Karlstrom
Feb 10 '17 at 8:46
|
show 12 more comments
sh: http://man.cx/shbash: 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.
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
add a comment |
sh: http://man.cx/shbash: 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.
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
add a comment |
sh: http://man.cx/shbash: 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.
sh: http://man.cx/shbash: 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.
edited Mar 9 '18 at 14:00
haccks
85.8k20126218
85.8k20126218
answered Apr 20 '11 at 3:43
Rein HenrichsRein Henrichs
12.3k3545
12.3k3545
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
add a comment |
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
27
27
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
if bash is invoked as sh, it behaves a bit differently. See gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files ("Invoked with name sh") and gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode. For example, no process substitution.
– glenn jackman
Apr 20 '11 at 4:12
9
9
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
As bash is a superset of sh and some OS like FreeBSD do not have bash installed by default, scripting in sh will give greater portability.
– user674062
Apr 20 '11 at 5:49
1
1
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
As there is no portable scriptable way to get a POSIX shell for a specific script, portable scripts cannot assume more than Bourne Shell features.
– schily
Sep 12 '15 at 21:34
1
1
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
On Ubuntu 16.04.3 LTS the command sh --version fails.
– Argent
Dec 18 '17 at 9:56
add a comment |
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
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.
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
|
show 4 more comments
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
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.
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
|
show 4 more comments
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
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.
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
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.
edited Dec 9 '16 at 8:17
Chaminda Bandara
85011225
85011225
answered Aug 4 '15 at 6:10
SriniVSriniV
8,602134565
8,602134565
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
|
show 4 more comments
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
Your table is not useful to me as it tries to compare features of the Bourne Shell and features from ksh from before 1988. If you really make a table for 1988, you would need to remove most of the other shells from that table - including bash, sh and rc. Could you explain where did yo get the values for your table from?
– schily
Sep 12 '15 at 20:43
1
1
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
Let me give some hints: Job Control was added to the Bourne Shell in 1989 and the Bourne Shell was made OpenSource in 2005. The Korn shell has process substitution since at least 1988 and it is OpenSource since 1997. BTW: your statements regarding $ENV are not correct, $ENV is only read/executed for interactive shells.
– schily
Sep 12 '15 at 20:47
3
3
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily This post has been captured from cs.virginia.edu/helpnet/Computer_OS/unix/shells/shelldiff.html
– SriniV
Sep 14 '15 at 5:35
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
@schily If you feel it is incorrect anywhere, please feel free to edit it appropriately.
– SriniV
Sep 14 '15 at 5:36
7
7
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
Based on what schily exposed it would seem that it would be better to remove this answer, as it is essentially fraudulent, and OP didn't really vet the information he pasted.
– danno
Oct 17 '15 at 0:12
|
show 4 more comments
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.
- If you run your script with
sh scriptname, or run it withscriptnameand have#!/bin/shin the shebang line, you should expect POSIXshbehavior. - If you run your script with
bash scriptname, or run it withscriptnameand 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).
shdoes not have arrays.- Some Bash keywords like
local,function, andselectare not portable tosh. - Bash has many C-style syntax extensions like
$'stringnwithtCaescapes'and the three-argumentfor((i=0;i<=3;i++))loop,+=increment assignment, etc. - Bash supports
<<<'here strings'. - Bash has
*.{png,jpg}and{0..9}brace expansion.
This is in POSIX, but may be mising from some pre-POSIX~refers to$HOMEonly in Bash (and more generally~usernameto the home directory ofusername)./bin/shimplementations.- Bash has process substitution with
<(cmd)and>(cmd). - Bash supports coprocesses with
<>redirection. - Bash has significantly extended facilities for shell arithmetic (though still no floating-point support) and variable substring manipulation with
${substring:1:2},${variable/pattern/replacement}, case conversion, etc. - Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
- Many, many convenience features for interactive use which however do not affect script behavior.
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.
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
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
add a comment |
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.
- If you run your script with
sh scriptname, or run it withscriptnameand have#!/bin/shin the shebang line, you should expect POSIXshbehavior. - If you run your script with
bash scriptname, or run it withscriptnameand 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).
shdoes not have arrays.- Some Bash keywords like
local,function, andselectare not portable tosh. - Bash has many C-style syntax extensions like
$'stringnwithtCaescapes'and the three-argumentfor((i=0;i<=3;i++))loop,+=increment assignment, etc. - Bash supports
<<<'here strings'. - Bash has
*.{png,jpg}and{0..9}brace expansion.
This is in POSIX, but may be mising from some pre-POSIX~refers to$HOMEonly in Bash (and more generally~usernameto the home directory ofusername)./bin/shimplementations.- Bash has process substitution with
<(cmd)and>(cmd). - Bash supports coprocesses with
<>redirection. - Bash has significantly extended facilities for shell arithmetic (though still no floating-point support) and variable substring manipulation with
${substring:1:2},${variable/pattern/replacement}, case conversion, etc. - Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
- Many, many convenience features for interactive use which however do not affect script behavior.
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.
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
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
add a comment |
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.
- If you run your script with
sh scriptname, or run it withscriptnameand have#!/bin/shin the shebang line, you should expect POSIXshbehavior. - If you run your script with
bash scriptname, or run it withscriptnameand 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).
shdoes not have arrays.- Some Bash keywords like
local,function, andselectare not portable tosh. - Bash has many C-style syntax extensions like
$'stringnwithtCaescapes'and the three-argumentfor((i=0;i<=3;i++))loop,+=increment assignment, etc. - Bash supports
<<<'here strings'. - Bash has
*.{png,jpg}and{0..9}brace expansion.
This is in POSIX, but may be mising from some pre-POSIX~refers to$HOMEonly in Bash (and more generally~usernameto the home directory ofusername)./bin/shimplementations.- Bash has process substitution with
<(cmd)and>(cmd). - Bash supports coprocesses with
<>redirection. - Bash has significantly extended facilities for shell arithmetic (though still no floating-point support) and variable substring manipulation with
${substring:1:2},${variable/pattern/replacement}, case conversion, etc. - Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
- Many, many convenience features for interactive use which however do not affect script behavior.
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.
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
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.
- If you run your script with
sh scriptname, or run it withscriptnameand have#!/bin/shin the shebang line, you should expect POSIXshbehavior. - If you run your script with
bash scriptname, or run it withscriptnameand 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).
shdoes not have arrays.- Some Bash keywords like
local,function, andselectare not portable tosh. - Bash has many C-style syntax extensions like
$'stringnwithtCaescapes'and the three-argumentfor((i=0;i<=3;i++))loop,+=increment assignment, etc. - Bash supports
<<<'here strings'. - Bash has
*.{png,jpg}and{0..9}brace expansion.
This is in POSIX, but may be mising from some pre-POSIX~refers to$HOMEonly in Bash (and more generally~usernameto the home directory ofusername)./bin/shimplementations.- Bash has process substitution with
<(cmd)and>(cmd). - Bash supports coprocesses with
<>redirection. - Bash has significantly extended facilities for shell arithmetic (though still no floating-point support) and variable substring manipulation with
${substring:1:2},${variable/pattern/replacement}, case conversion, etc. - Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
- Many, many convenience features for interactive use which however do not affect script behavior.
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.
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
edited Nov 4 '18 at 16:57
answered Mar 8 '17 at 8:44
tripleeetripleee
89.1k12124181
89.1k12124181
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
add a comment |
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
1
1
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
Fundamentally, the TL;DR ersion is And's answer.
– tripleee
Mar 8 '17 at 8:47
1
1
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
shellcheck.net was all I needed. many thanks.
– Josh Habdas
Jul 30 '18 at 10:33
add a comment |
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:
- Shell is like an interface or specifications or API.
- sh is a class which implements the Shell interface.
- Bash is a subclass of the sh.

2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
I think this is trying to say Bash inherits fromsh(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).
– tripleee
May 19 '18 at 4:44
add a comment |
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:
- Shell is like an interface or specifications or API.
- sh is a class which implements the Shell interface.
- Bash is a subclass of the sh.

2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
I think this is trying to say Bash inherits fromsh(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).
– tripleee
May 19 '18 at 4:44
add a comment |
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:
- Shell is like an interface or specifications or API.
- sh is a class which implements the Shell interface.
- Bash is a subclass of the sh.

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:
- Shell is like an interface or specifications or API.
- sh is a class which implements the Shell interface.
- Bash is a subclass of the sh.

edited Sep 6 '18 at 2:04
answered Feb 9 '15 at 9:46
PremrajPremraj
29.1k10152116
29.1k10152116
2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
I think this is trying to say Bash inherits fromsh(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).
– tripleee
May 19 '18 at 4:44
add a comment |
2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
I think this is trying to say Bash inherits fromsh(so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).
– tripleee
May 19 '18 at 4:44
2
2
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
I don't get it. You've mentioned both "Bash is superset of sh" and "Bash is a subclass of the sh", aren't they contrary statements? Can you please clarify?
– Keerthana Prabhakaran
Apr 26 '18 at 6:13
3
3
I think this is trying to say Bash inherits from
sh (so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).– tripleee
May 19 '18 at 4:44
I think this is trying to say Bash inherits from
sh (so it's a "subclass" in the OOP sense) and extends it (so has a superset of the functionality).– tripleee
May 19 '18 at 4:44
add a comment |
TERMINAL
- program(s) that put a window up
- xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
SHELL
- Is a program that runs in the terminal
- Shell is both a command interpreter and a programming language
- Shell is simply a macro processor that executes commands.
- Macro processor means functionality where text and symbols are expanded to create larger expressions.
SH Vs. BASH
SH
- (SHell)
- Is a specific shell
- a command interpreter and a programming language
- Predecessor of BASH
BASH
- (Bourne-Again SHell)
- Is a specific shell
- a command interpreter and a programming language
- Has sh functionality and more
- Successor of SH
- BASH is the default SHELL
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.
add a comment |
TERMINAL
- program(s) that put a window up
- xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
SHELL
- Is a program that runs in the terminal
- Shell is both a command interpreter and a programming language
- Shell is simply a macro processor that executes commands.
- Macro processor means functionality where text and symbols are expanded to create larger expressions.
SH Vs. BASH
SH
- (SHell)
- Is a specific shell
- a command interpreter and a programming language
- Predecessor of BASH
BASH
- (Bourne-Again SHell)
- Is a specific shell
- a command interpreter and a programming language
- Has sh functionality and more
- Successor of SH
- BASH is the default SHELL
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.
add a comment |
TERMINAL
- program(s) that put a window up
- xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
SHELL
- Is a program that runs in the terminal
- Shell is both a command interpreter and a programming language
- Shell is simply a macro processor that executes commands.
- Macro processor means functionality where text and symbols are expanded to create larger expressions.
SH Vs. BASH
SH
- (SHell)
- Is a specific shell
- a command interpreter and a programming language
- Predecessor of BASH
BASH
- (Bourne-Again SHell)
- Is a specific shell
- a command interpreter and a programming language
- Has sh functionality and more
- Successor of SH
- BASH is the default SHELL
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.
TERMINAL
- program(s) that put a window up
- xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
SHELL
- Is a program that runs in the terminal
- Shell is both a command interpreter and a programming language
- Shell is simply a macro processor that executes commands.
- Macro processor means functionality where text and symbols are expanded to create larger expressions.
SH Vs. BASH
SH
- (SHell)
- Is a specific shell
- a command interpreter and a programming language
- Predecessor of BASH
BASH
- (Bourne-Again SHell)
- Is a specific shell
- a command interpreter and a programming language
- Has sh functionality and more
- Successor of SH
- BASH is the default SHELL
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.
edited Sep 3 '16 at 18:53
answered Sep 3 '16 at 12:51
Timothy L.J. StewartTimothy L.J. Stewart
667610
667610
add a comment |
add a comment |
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.
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
add a comment |
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.
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
add a comment |
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.
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 May 18 '15 at 2:22
Andrzej PronobisAndrzej Pronobis
17.2k94566
17.2k94566
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
add a comment |
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
This is basically what my answer as of just now really boils down to. +1
– tripleee
Mar 8 '17 at 8:46
add a comment |
/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.
shonly gives you a POSIX shell, if you have the rightPATHset up in your current shell. There is no defined PATH-name that gives you a POSIX shell.
– schily
Sep 12 '15 at 21:37
For a long time,shwas not necessarily even giving you a POSIX shell, on Solaris, for example.
– tripleee
Apr 14 '16 at 4:19
add a comment |
/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.
shonly gives you a POSIX shell, if you have the rightPATHset up in your current shell. There is no defined PATH-name that gives you a POSIX shell.
– schily
Sep 12 '15 at 21:37
For a long time,shwas not necessarily even giving you a POSIX shell, on Solaris, for example.
– tripleee
Apr 14 '16 at 4:19
add a comment |
/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.
/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 Apr 9 '15 at 16:01
Keith ThompsonKeith Thompson
190k25282470
190k25282470
shonly gives you a POSIX shell, if you have the rightPATHset up in your current shell. There is no defined PATH-name that gives you a POSIX shell.
– schily
Sep 12 '15 at 21:37
For a long time,shwas not necessarily even giving you a POSIX shell, on Solaris, for example.
– tripleee
Apr 14 '16 at 4:19
add a comment |
shonly gives you a POSIX shell, if you have the rightPATHset up in your current shell. There is no defined PATH-name that gives you a POSIX shell.
– schily
Sep 12 '15 at 21:37
For a long time,shwas not necessarily even giving you a POSIX shell, on Solaris, for example.
– tripleee
Apr 14 '16 at 4:19
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.– schily
Sep 12 '15 at 21:37
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.– schily
Sep 12 '15 at 21:37
For a long time,
sh was not necessarily even giving you a POSIX shell, on Solaris, for example.– tripleee
Apr 14 '16 at 4:19
For a long time,
sh was not necessarily even giving you a POSIX shell, on Solaris, for example.– tripleee
Apr 14 '16 at 4:19
add a comment |
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 include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
add a comment |
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 include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
add a comment |
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 include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
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 include many scripting tools.
Bash shell :
Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most linux distribution and substitute for Sh Shell (Sh shell will also run in the Bash shell) . Bash Shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.
answered Jun 25 '18 at 13:37
rashedcsrashedcs
1,0791224
1,0791224
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5725296%2fdifference-between-sh-and-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
19
For a useful list of bashisms and corresponding code that works on Bourne shell, see mywiki.wooledge.org/Bashism
– dancek
Apr 20 '11 at 4:14
You may want to see the POSIX standard for sh and its command language: * sh * Shell Command Language
– Maurício C Antunes
Jul 23 '13 at 3:01
4
as a general rule, all sh scripts will run under bash thanks to it's posix compatibility, but not all bash scripts can run under sh, the main differences you notice are things like [[ ]] instead of [ ] comparisons which allow unquoted spaces, $(( )) instead of $[ ] arithmetic expressions, and other things like "its too big and too slow" directly from the bash docs.. But new scripters need not limit themselves to sh-compatible scripts unless they are shooting for some backward compatibility, which more often than not is not the case these days, after all it is (or was...) the year 2014 right??
– osirisgothra
Mar 12 '14 at 14:03