Why do you need to put #!/bin/bash at the beginning of a script file?

node ninja picture node ninja · Jan 23, 2012 · Viewed 648.3k times · Source

I have made Bash scripts before and they all ran fine without #!/bin/bash at the beginning.

What's the point of putting it in? Would things be any different?

Also, how do you pronounce #? I know that ! is pronounced as "bang."

How is #! pronounced?

Answer

paulsm4 picture paulsm4 · Jan 23, 2012

It's a convention so the *nix shell knows what kind of interpreter to run.

For example, older flavors of ATT defaulted to sh (the Bourne shell), while older versions of BSD defaulted to csh (the C shell).

Even today (where most systems run bash, the "Bourne Again Shell"), scripts can be in bash, python, perl, ruby, PHP, etc, etc. For example, you might see #!/bin/perl or #!/bin/perl5.

PS: The exclamation mark (!) is affectionately called "bang". The shell comment symbol (#) is sometimes called "hash".

PPS: Remember - under *nix, associating a suffix with a file type is merely a convention, not a "rule". An executable can be a binary program, any one of a million script types and other things as well. Hence the need for #!/bin/bash.