I'm very new to Ubuntu. I hear people say "shell scripting", "bash scripting".
I wonder are they same? Or they are different?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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 (
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.
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
C shell (csh) and the
Z shell (zsh), but we will not go into them here as a discussion of
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.
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
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/bash. Shell scripts can also be called on the command line with either
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
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.
$ ls -l /bin/sh lrwxrwxrwx 1 root root 4 Mar 29 11:53 /bin/sh -> dash* $ ls -l /bin/dash -rwxr-xr-x 1 root root 109768 Mar 29 11:53 /bin/dash* $ ls -l /bin/bash -rwxr-xr-x 1 root root 955024 Apr 3 08:58 /bin/bash*
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:
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
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 to
bin/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-bash
Debian/Ubuntu interactive terminal is by default, as in many other Unix-like operating systems, bash, for which the path is also uniform:
POSIX.1-2017 standard: http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
If a command or option isn't defined by POSIX, then don't put it in a
#! /bin/shscript in the first place.
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: