(#!/bin/bash ) What exactly is this ?

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?

#!/bin/bashecho $(date) # Will print the output of date commandtouch ~/output.txtecho “Hey there” > ~/output.txt

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:

#!/bin/sh

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.