ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    What Exactly is a Sh-bang?

    IT Discussion
    linux scripting
    3
    3
    728
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by

      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.

      1 Reply Last reply Reply Quote 1
      • travisdh1T
        travisdh1
        last edited by

        The sh-bang simply explained, I may be linking to this often.

        1 Reply Last reply Reply Quote 1
        • RojoLocoR
          RojoLoco
          last edited by

          You all know what sh-bang really is....

          Youtube Video

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post