What is the difference between #!/bin/sh and #!/bin/bash?
This is especially true for the users of Linux and its distros. Using the command terminal for performing regular tasks is pretty common.
The programs working behind the scenes of the command terminal are called “shells”. Shells are defined as programs that take in commands, interpret them, and then instruct the computer to perform the function the user wants it to do. So, everything you code into is part of a shell.
In Linux, users use the programming language called “sh” to code into the shell. “Sh” is also known as “Bourne Shell,” It was developed to be used with UNIX systems defined using the POSIX standards.
“Bash” is similarly a shell programming language. It is also known as “Bourne Again Shell”. Today, Bash is the default programming language for the shells in Linux. Bash has the same abilities as Shell, but it developed other functions and better extensions over time.
If you are a Linux user and you want to know the difference between Shell and Bash, you have come to the right place as we will be going deep and describing the differences between the two shell programming languages.
Bin/sh
Till now, we have established that “sh” is called “Shell,” and it is a shell programming language for UNIX-based systems. We also said that it follows the POSIX standards. POSIX standards were developed by IEEE to make different operating systems compatible. Thus, POSIX standards ensure that anything developed using shell can be used in other operating systems as well.
The shell can be better described as a specification, not an implementation. An implementation can be described as a complete programming language with its own separate compilers, such as C and C++.
Shell is not like the traditional programming language. Thus, it can’t be called an implementation. Shell describes the syntax and the semantics to be used to program in the Linux shell in detail.
For different operating systems, the shell can be linked with different implementations. For example, Shell has the “dash” implementation in Linux, the “Z shell” for Mac, and “ash” for OpenWRT.
Bin/bash
Bash can be called an implementation of Shell developed for the GNU project. As mentioned before, it includes all the shell features, but it developed better extensions. Hence, it can also be called a superset of Shell, including ideas drawn from other shells such as the C shell.
Bash can be used for both scripting and implementation. In the implementing mode, bash takes the form of the command terminal of today’s Linux. For Ubuntu, the scripting shell is dash, and the default terminal is bash.
As it is a superset, all features of Bash are not supported by other shells. Bash can execute most of the tasks carried out by other shells, but it is not true for the opposite. Other shells have limitations, and they don’t have many features provided by Bash.
Some of the features that are Bash exclusive are as follows.
- Process substitution
- Scoping variables
- Arrays
- Bash supports many special variables such as $RANDOM, $SECONDS, etc.
Advantages of Using Shell
Shell scripting is done using all types of shells out there. As Bash is the most common and most used, there are obvious advantages of bash over shell. But, there is a big advantage when using Shell over Bash, which is portability.
As shell follows the POSIX standards, a script written in Shell can be used in every Linux system without any problems. The same can’t be said for Bash. As Bash added better extensions and features, it slowly went away from the POSIX standards, thus affecting its portability.
Advantages of Using Bash
As mentioned before, Bash has more advanced features than Shell. Having more advanced features just makes Bash more usable and effective in its functioning. That is why Bash is more commonly used as compared to Shell.
A big advantage of Bash is that it can easily be debugged as compared to Shell. Finding errors and fixing scripts in Shell are considerably harder than finding errors in a script written in Bash.
We mentioned that Bash strayed away from the POSIX standards. But, if a user wants to make Bash more POSIX supportive, Bash offers a POSIX switch feature. It can be enabled by typing the following command.
Conclusion
This article discussed what shells are and the languages users can use to write shell scripts. The two major languages that came forward were Shell and Bash. We deeply described both of them so we could explain the differences properly. Bash is the newer and improved version of Shell and thus gets our vote as the better shell language. The features offered and the advantages of Bash over Shell are very heavy. Thus, choosing Bash over Shell to write our Shell scripts is easy.
We hope that we can explain the differences clearly and that you now know what you’re dealing with when using either Shell or Bash. We also hope that we chose between Shell and Bash easier for you.
What is the difference between #!/bin/sh and #!/bin/bash?
both yields same. I have seen some scripts starting with #!/bin/sh or #!/bin/bash . Is there any difference between them?
![]()
4 Answers 4
bash and sh are two different shells. Basically bash is sh , with more features and better syntax. Most commands work the same, but they are different.
Having said that, you should realize /bin/sh on most systems will be a symbolic link and will not invoke sh . In Ubuntu /bin/sh used to link to bash , typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash , as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh because the script-maker assumes the link is to bash when it doesn’t have to be.
On Linux and other Unix-like systems you have a choice of multiple shells.
The shell is responsible not only for drawing your little prompt, but interpreting your commands, especially if you put in complicated logic like pipes, conditionals and so on.
bash is the most common shell used as a default shell for users of Linux systems. It is a spiritual descendent of other shells used throughout Unix history. Its name, bash is an abbreviation of Bourne-Again Shell, an homage to the Bourne shell it was designed to replace, though it also incorporates features from the C Shell and the Korn Shell.
It’s run, these days, from /bin/bash — any system with bash will have it accessible here.
It’s not just users that use shells, though. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script.
The problem is, different shells have tiny little inconsistencies between them, and when it comes to running scripts, these can be a real problem. bash has quite a lot of scripting features that are unique only to bash, and not to other shells. This is fine, if you’re always going to use bash to run those scripts. Other shells may try to either emulate bash, or adhere to the POSIX standard, which bash supports pretty well (though adds its own extensions to).
It’s possible to specify at the top of a shell script which shell it should be run with using a shebang. A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell.
/bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is kind of the default shell that system scripts should use. In Linux distributions, for a long time this was usually a symbolic link to bash, so much so that it has become somewhat of a convention to always link /bin/sh to bash or a bash-compatible shell. However, in the last couple of years Debian (and Ubuntu) decided to switch the system shell from bash to dash — a similar shell — breaking with a long tradition in Linux (well, GNU) of using bash for /bin/sh. Dash is seen as a lighter, and much faster, shell which can be beneficial to boot speed (and other things that require a lot of shell scripts, like package installation scripts).
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn’t implement the bash-specific extensions. There are scripts in existence that use #!/bin/sh (the system shell) as their shebang, but which require bash-specific extensions. This is currently considered a bug that should be fixed by Debian and Ubuntu, who require /bin/sh to be able to work when pointed to dash.
Even though Ubuntu’s system shell is pointing to dash, your login shell as a user continues to be bash at this time. That is, when you log in to a terminal emulator anywhere in Linux, your login shell will be bash. Speed of operation is not so much a problem when the shell is used interactively, and users are familiar with bash (and may have bash-specific customisations in their home directory).
What you should use when writing scripts
If your script requires features only supported by bash, use #!/bin/bash .
But if at all possible, it would be good to make sure your script is POSIX-compatible, and use #!/bin/sh , which should always, quite reliably, point to the preferred POSIX-compatible system shell in any installation.
WHAT IS THIS LINE CALLED?
This first line (#!/bin/bash or #!/bin/sh) has a name. It is known as ‘she-bang‘(shabang). This derives from the concatenation of the tokens sharp (#) and bang (!). It is also called as sh-bang, hashbang, poundbang or hash-pling. In computing, a she-bang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script.
/bin/bash VS /bin/sh
We have often seen variety of she-bang or script header. We often wonder why is that particular script using that particular she-bang, why not some other. On Unix-like Operating systems we have a choice of multiple shells. The shell is responsible not only for the little prompts but also interpreting the commands of the script. Thus the shell plays an important role specially when we implement big and complex logics using conditions, pipes, loops , etc.
/bin/sh is an executable representing the system shell and usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell. The system shell is basically the default shell that the script should use. In last couple of years, Debian (and Ubuntu) decided to switch the system shell from bash to dash — a similar shell but lighter and much faster.
Dash is fairly well compatible with bash, being based on the same POSIX standard. However, it doesn’t implement the bash-specific extensions. POSIX standard is Portable Operating System Interface, an attempt to standardize UNIX-like OSes. Even though Ubuntu’s system shell is pointing to dash, your login shell as a user continues to be bash at this time.
/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax.
WHAT IS IT ? / WHY DO WE USE IT?
Let’s consider a very simple script as above. In any simplest case if we analyse a shell script, it is nothing but a list of commands stored in a file. It reduces our effort to run the same task or commands again and again. So if we look at the beginning of the script the first line starts with a hash (#) and an exclamation mark (!). As you already must be knowing that any line starting with a hash (#) , is read as a comment. Thus when we execute the script, the first line is read as a comment and interpreter goes to the second line. But the first line has already done it’s job.
In Unix-like Operating Systems when a script starting with a she-bang(#!) is executed as a program, the program loader parses the rest of the script’s initial line as a interpreter-directive. Thus the specified interpreter program is run instead, passing to it as an argument the path that was used initially through the script.
Suppose any script starts with the following line:
then the program loader is instructed to use the /bin/sh program instead of any other, passing the path of the script as the first argument.
In simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated. Unix-like operating systems has variety of shells and each of script header lines call a different command interpreter.
SOME she-bang EXAMPLES
#!/bin/sh :Executes the script using the Bourne shell or a compatible shell, with path /bin/sh
#!/bin/bash :Executes the script using the Bash shell.
#!/bin/csh -f :Executes the script using C shell or a compatible shell.
#!/usr/bin/perl -T :Executes the script using perl with the option of taint checks
#!/usr/bin/env python :Executes the script using python by looking up the path to the python interpreter automatically from the environment variables
Hopefully you have better idea of what she-bang is now and what purpose does it serve.
What does the line "#!/bin/sh" mean in a UNIX shell script?
I was going through some shell script tutorials and found the following sample program:
Can anyone please tell me what the significance of the comment #!/bin/sh at the start is?
![]()
![]()
5 Answers 5
It’s called a shebang, and tells the parent shell which interpreter should be used to execute the script.
Most scripting languages tend to interpret a line starting with # as comment and will ignore the following !/usr/bin/whatever portion, which might otherwise cause a syntax error in the interpreted language.
When you try to execute a program in unix (one with the executable bit set), the operating system will look at the first few bytes of the file. These form the so-called «magic number», which can be used to decide the format of the program and how to execute it.
#! corresponds to the magic number 0x2321 (look it up in an ascii table). When the system sees that the magic number, it knows that it is dealing with a text script and reads until the next \n (there is a limit, but it escapes me atm). Having identified the interpreter (the first argument after the shebang) it will call the interpreter.
Other files also have magic numbers. Try looking at a bitmap (.BMP) file via less and you will see the first two characters are BM . This magic number denotes that the file is indeed a bitmap.