Welcome Linux users. We often come across this line #!/bin/bash in our day to day work while scripting and other Linux tasks. For many people it just becomes a standard to start any shell script with this line but it is important to understand the reason behind it.

In this post I will help you understand why do we use this line and what are other important things related to it.

 

WHAT IS THIS LINE CALLED?

This first line (#!/bin/bash or #!/bin/sh)  has a name. It is known as ‘she-bang‘. 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/bash
echo $(date) # Will print the output of date command
touch ~/output.txt
echo "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.

I hope this post has been helpful to you. Comment below any issues faced and I will try best to provide you the solution as soon as possible. Feel free to provide suggestions.

Connect with me on LinkedIn by clicking here.

 

 

One thought on “(#!/bin/bash ) What exactly is this ?

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.