3 Answers
3

That is called a shebang, it tells the shell what program to interpret the script with, when executed.

In your example, the script is to be interpreted and run by the bash shell.

Some other example shebangs are:

(From Wikipedia)

#!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
#!/bin/csh — Execute the file using csh, the C shell, or a compatible shell
#!/usr/bin/perl -T — Execute using Perl with the option for taint checks
#!/usr/bin/php — Execute the file using the PHP command line interpreter
#!/usr/bin/python -O — Execute using Python with optimizations to code
#!/usr/bin/ruby — Execute using Ruby

and a few additional ones I can think off the top of my head, such as:

#!/bin/ksh
#!/bin/awk
#!/bin/expect

In a script with the bash shebang, for example, you would write your code with bash syntax; whereas in a script with expect shebang, you would code it in expect syntax, and so on.

Response to updated portion:

It depends on what /bin/sh actually points to on your system. Often it is just a symlink to /bin/bash. Sometimes portable scripts are written with #!/bin/sh just to signify that it's a shell script, but it uses whichever shell is referred to by /bin/sh on that particular system (maybe it points to /bin/bash, /bin/ksh or /bin/zsh)

Thanks! But is there a difference between #!/bin/bash and #!/bin/sh ?
–
RaptorDec 14 '12 at 3:07

@ShivanRaptor #!/bin/bash Means run this script in bash. #!/bin/sh means run this script in sh which is the default unix shell, which might be bash or any other variant like ksh, dash, zsh, etc
–
Karthik TDec 14 '12 at 3:10

When bash is run as sh, it behaves differently (more POSIX-like) than when it is run as bash. Read the manual; it does cover that detail.
–
Jonathan LefflerDec 14 '12 at 3:14

@KarthikT it doesn't run the default shell, it runs bash in Bourne Shell mode. (as opposed to Bourne Again Shell, aka. bash) On some older systems it would run Bourne Shell itself.
–
SlomojoDec 14 '12 at 3:48

@Slomojo i see, i assumed it was just a simple symlink..
–
Karthik TDec 14 '12 at 4:48

When the first characters in a script are #!, that is called the shebang. If your file starts with
#!/path/to/something the standard is to run something and pass the rest of the file to that program as an input.

With that said, the difference between #!/bin/bash, #!/bin/sh, or even #!/bin/zsh is whether the bash, sh, or zsh programs are used to interpret the rest of the file. bash and sh are just different programs, traditionally. On some linux systems they are two copies of the same program. On other linux systems, sh is a link to dash, and on traditional unix systems (Solaris, Irix, etc) bash is usually a completely different program from sh.

Of course, the rest of the line doesn't have to end in sh. It could just as well be #!/usr/bin/python, #!/usr/bin/perl, or even #!/usr/local/bin/my_own_scripting_language.

sh is usually bash running a compatibility mode, this has been true since bash became the default shell for most Linux distro's, and IIRC for a good few years before that, safe to say it's been 15 years, possibly more than 20.
–
SlomojoDec 14 '12 at 3:51

@JoshuaDBoyd nice catch, many (although probably not all) BSD unixes use Ash, as their default shell, as opposed to Bash, and Debian / Ubuntu use Dash. Likewise with Bash, both Ash & Dash have sh compatibility modes. If we delve into Solaris, Aix, HPUX, Ultrix, SCO etc. each will have their own default shell, which will have sh emulation (to remain POSIX compliant.)
–
SlomojoDec 14 '12 at 6:19

In Linux system, we have shell which interprets our UNIX commands. Now there are a number of shell in Unix system. Among them, there is a shell called bash which is very very common Linux and it has a long history. This is a by default shell in Linux.

When you write a script (collection of unix commands and so on) you have a option to specify which shell it can be used. Generally you can specify which shell it wold be by using Shebang(Yes that's what it's name).

So if you #!/bin/bash in the top of your scripts then you are telling your system to use bash as a default shell.

Now coming to your second question :Is there a difference between #!/bin/bash and #!/bin/sh ?

The answer is Yes. When you tell #!/bin/bash then you are telling your environment/ os to use bash as a command interpreter. This is hard coded thing.

Every system has its own shell which the system will use to execute its own system scripts. This system shell can be vary from OS to OS(most of the time it will be bash. Ubuntu recently using dash as default system shell). When you specify #!/bin/sh then system will use it's internal system shell to interpreting your shell scripts.

Visit this link for further information where I have explained this topic.