What Exactly is a Sh-bang?
-
It is common in UNIX systems to have scripts start with a sh-bang, written #!. The # denotes a comment and is sometimes called a "sh" (I guess because it is telling the line to be quiet) and an exclamation point is commonly called a "bang" character. So the #! combination is called a sh-bang (pronounced roughly shebang with a soft e.)
Why do we have this at the top of many scripts? It looks like this...
#!/bin/bash
Or...
#!/bin/ruby
This line is a non-executable comment that is used to tell the calling shell what environment the script is written for. Let's say we are running in ksh and we call a script natively (that is running it directly without calling a language to execute it), how will it know how to run the script? It looks at the first line to see if there is a sh-bang. If there is, it will use the path denoted there to execute the script. Without it, it can't run it.
The traditional way to run a script is to pass the script as input to a scripting language. To run a ruby program we would do something like this...
ruby /tmp/myscript.rb
To run a BASH script we would do this...
bash /tmp/myscript.sh
But if we add in the sh-bang we need only do this...
/tmp/myscript.sh
And the shell environment figures out how to run it and runs it on its own making us, as the script users, not need to memorize or look up the language needed for every script that we want to run.
-
The sh-bang simply explained, I may be linking to this often.
-