Is bash scripting the same as shell scripting?
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 as sh
that can be used to execute a script; a script intended to be executed by Bash
should be labelled as a Bash
script. The terms are often used interchangeably because Bash
, with its extended functionality as compared to sh
, is the one most commonly used to execute user scripts in many distributions. However, there are other shells such as the Korn (ksh)
, C shell (csh)
and the Z shell (zsh)
, but we will not go into them here as a discussion of sh
and bash
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 considers dash
as their sh
shell (sh
is actually symlinked to dash
). In Debian and Ubuntu, because of the speed of sh
, 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 original sh
. Fox and others' development of Bash
was an important part of the GNU project. See this great discussion of the history of Bash
for more information.
It is important to note that both sh
and Bash
, as used in Ubuntu and other distributions, are POSIX
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 those POSIX
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 as bash
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 either sh
or bash
.
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 in sh
, whereas most sh
commands will work in bash
. 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 on Bash
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
and csh
.
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 using bash
. But as bash
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.