I'm very new to Ubuntu. I hear people say "shell scripting", "bash scripting".
I wonder are they same? Or they are different?
I'm very new to Ubuntu. I hear people say "shell scripting", "bash scripting".
I wonder are they same? Or they are different?
Bash (
bash
) is one of many available (yet the most commonly used) Unix shells. Bash stands for "Bourne Again SHell", and is a replacement/improvement of the original Bourne shell (sh
).Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. In practice, however, "shell script" and "bash script" are often used interchangeably, unless the shell in question is not Bash.
EDIT: Actually, the default scripting shell in Ubuntu is dash, while the default interactive shell (what you get if you pull up a terminal) is Bash. Nonetheless, the two terms are still mostly interchangeable.
Introduction
Shell scripting and
Bash
scripting are not the same thing, as other shells exist such assh
that can be used to execute a script; a script intended to be executed byBash
should be labelled as aBash
script. The terms are often used interchangeably becauseBash
, with its extended functionality as compared tosh
, is the one most commonly used to execute user scripts in many distributions. However, there are other shells such as theKorn (ksh)
,C shell (csh)
and theZ shell (zsh)
, but we will not go into them here as a discussion ofsh
andbash
is most relevant to Ubuntu. A great IBM article here goes into more detail on the evolution of shells in Linux and describes well the shell architecture and how shells differ.Shell Scripting
Sh
was the original Unix shell developed by Stephen Bourne; however Debian based systems and Ubuntu considersdash
as theirsh
shell (sh
is actually symlinked todash
). In Debian and Ubuntu, because of the speed ofsh
, it is more often used for critical system procedures and for executing key scripts at startup; for more detail see the Ubuntu wiki.Bash
stands for the Bourne Again SHell and was developed later by Brian Fox and much extended the originalsh
. Fox and others' development ofBash
was an important part of the GNU project. See this great discussion of the history ofBash
for more information.It is important to note that both
sh
andBash
, as used in Ubuntu and other distributions, arePOSIX
compliant, which means they subscribe to a number of standards about how commands are executed in the Shell. This is to ensure that the results of scripts used in the OS can be predicted reliably, and that the behaviour of the shell can be kept within thosePOSIX
parameters, as that is particularly important for developers. For more information on the standards, see the official documentation.Often shell scripts have the suffix
.sh
, even though they are intended to be executed asbash
scripts, and have#!/bin/bash
at the top of the script. It actually does not matter whether the script is called script.sh or my.script, what matters is whether the call to the interpreter is/bin/sh
or/bin/bash
. Shell scripts can also be called on the command line with eithersh
orbash
.However, it is important to note that the results can be different depending on which interpreter is called, as not all
bash
commands will work insh
, whereas mostsh
commands will work inbash
. In general, most users will want to use/bin/bash
for their scripts so they can take advantage of the extended feature set; system scripts can be executed with/bin/sh
if it is required.Resources for Bash Shell scripting
It is sometimes difficult to find useful resources online that follow good practice and that give advice that will allow you to create useful scripts. After
man bash
, some of the most important resources are Greg's wiki, Bash hackers and Steve Parker's recent book on Shell scripting that focuses mainly onBash
and is published by O'Reilly. A good introduction is also undertaken by the Bash beginner's Guide.There are multiple shells available for Ubuntu, like
bash
,zsh
,ksh
,tcsh
andcsh
.So anytime someone says shell, he is talking about one of those. However, those shells differ a bit from each other. So, when someone talks about
bash
scripting, he's using a shell, but when someone is talking about shell scripting, he isn't per se usingbash
. But asbash
is commonly used in scripting for Ubuntu, he usually is. Moreover, the different shells are the same in lots of aspects, so it usually doesn't matter.Shell script would be defined as a portability oriented script which can be run by a system shell of POSIX-compliant OSs. Syntax would be identical or similar with the shell scripting language syntax defined by the POSIX standard. That is the standard for most POSIX compatible OSs, like Linux/Unix/*BSD etc. POSIX is the most common ground for compatibility across Operating Systems.
Different OSs from the aforementioned implement different shells for the non-interactive usage (ie. to execute system scripts or scripts that use
#! /bin/sh
shebang) which beside implementing POSIX commands and syntax, have their own extensions or can possibly be stripped out of some less useful features for performance reasons, but the POSIX ground allows for a very high level of portability of scripts designed for different POSIX-compliant Oss.Most aforementioned OSs have separate interactive shell which is usually fully-featured bash. Bash is POSIX-compliant in large part, but also has a big pool of additional commands and supports different syntax. Starting Bash with the --posix command-line option or executing ‘set -o posix’ while Bash is running will cause Bash to conform more closely to the POSIX standard by changing the behavior to match that specified by POSIX in areas where the Bash default differs, see: https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html
Shebang specifier and uniform paths for shell executables on Unix-like OSs
Thanks to the uniform rules on placing executables for shells (they are usually in '/bin/' directory), we can have uniform rules for creating shell scripts, more specifically, we are clear what path to put in the shebang expression to point at a proper shell executable to run the script. Unix/Linux/*BSD file systems don't support extensions intrinsically, so the file extensions serve only as an additional hint, or for indexing purposes.
On Debian/Ubuntu specifically,
bin/sh
is a symlink pointing tobin/dash
which is an executable of a dash shell. That makes dash the system shell, which is estimated to be 4x faster and is ~1/10 size than the more functional bash. source: https://unix.stackexchange.com/questions/148035/is-dash-or-some-other-shell-faster-than-bashDebian/Ubuntu interactive terminal is by default, as in many other Unix-like operating systems, bash, for which the path is also uniform:
/bin/bash
.POSIX.1-2017 standard: http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
General rule would be:
Convert your script or check for errors
For converting your script from bash to POSIX form, you may want to automatically check for bugs in your shell script or see what changes in your bash script you should make to make it POSIX compatible:
https://www.shellcheck.net/
http://mywiki.wooledge.org/Bashism
This shows that 'sh' is a symlink to 'dash', and that /bin/bash, which is the default interactive shell on Ubuntu, is an executable that is almost 9 time bigger than /bin/sh.
Indeed, 'man sh' (1590 lines) vs 'man bash' (5459 lines) reveals that bash is a large superset of the traditional 'sh'.
Read more here: