Necessity of shebang line

Hi ,
I know about the shebang line in shell scripting. Just want to know whether is there any difference in execution of the program by keeping and not keeping the shebang line. Because without shebang line also the script is working. correct me if am wrong. Any help on this will be helpful

Depends how you're running the script in the first place.

If you're running it with sh scriptname or the like, there is no difference, since the shebang is treated as a comment.

If you're running it with ./scriptname, then the shebang is important, since it chooses what shell you get. You can choose not only the various Bourne shells, but things like awk and perl. If you never use it, you're up to the mercy of the system of what shell you actually get. You could get anything from BASH(on Linux) to DASH(some other Linux), to a mouldy old pre-POSIX bourne(on Solaris).

Also, there was a recent, interesting discussion about the shebang.

Have a look at post 216 of the thread. It gets interesting from about post 205.

And if you have no shebang and use ./scriptname then the new shell used for executing the script will be like the current shell.

Interesting to note that if the script starts with "#!", the login shell uses what follows as the command to run followed by the name of the file itself. The command does not have to be a shell.

Given a file called x:

#!/bin/ls -l

print "Uh oh, Chongo!"

The login shell will run:

/bin/ls -l x

Output:

$ x
-rwxrwx--x   1 user group           37 May 31 09:47 x
$

Some systems only accept things listed in /etc/shells as login shells.