Thursday, October 5, 2017

What does the line #!/bin/sh mean in a UNIX shell script?

It's called a shebang, and tells the parent shell which interpreter should be used to execute the script.
e.g.
#!/usr/bin/perl   <--perl script'
#!/usr/bin/php <-- php script
#!/bin/false <--- do-nothing script, because false returns immediately anyways.

It's implemented as a comment so that anything coming in that line will not "relevant" to the interpreter specified. E.g. all scripting languages tend to understand that a line starting with # is a comment, and will ignore the !/usr/bin/whatever portion, which might otherwise be a syntax error in that particular language.

#! corresponds to the magic number 0x2321 (look it up in an ascii table). When the system sees that the magic number, it knows that it is dealing with a text script and reads until the next \n(there is a limit, but it escapes me atm).

1 comment:

  1. Mohegan Sun Arena: Schedule & Tickets - KTNV
    For all 광양 출장안마 your excitement, 경상북도 출장마사지 head over 제주도 출장안마 to the Mohegan Sun Arena and enjoy your favorite concerts and activities at Mohegan 과천 출장마사지 Sun Arena. Schedule your 충주 출장샵 event

    ReplyDelete