Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In many Apple/Unix scripts I see stuff preceeded by '#" which as far as I know means it is a remark. However often you see shell calls as below (the line with #!).

## Setting a default route 
#!/bin/sh

/sbin/route add SUBNET $5

I am wondering whether this is also just a comment or that it actually does something. Thanks for your experts' advice :-)

share|improve this question
1  
nice question! +1 –  studiohack Feb 23 '11 at 14:30
    
possible duplicate of What exactly interpret #!/bin/bash line? –  Chris Johnsen Mar 9 '11 at 3:56

2 Answers 2

It's a way of defining of which shell should be invoked during execution of the script. It's called the Shebang.

Example, a file having the following Shebang line:

#!/bin/sh

would invoke the Bourne (sh) shell. So for example, your piece of script:

## Setting a default route 
#!/bin/sh

/sbin/route add SUBNET $5

would make /sbin/route add SUBNET $5 run in the Bourne shell interpreter. /sbin/route add SUBNET $5 is just a comment and doesn't do anything. However, the shebang should be on the first line, therefore that comment at the top will disallow the shebang to do its work, making it a useless addition.

There's a number of interpreters that are widely used and some typical interpreters for shebang lines are:

  • #!/bin/sh — Execute using sh, the Bourne shell (or a compatible shell)
  • #!/bin/csh — Execute using csh, the C shell (or a compatible shell)
  • #!/usr/bin/perl — Execute using Perl
  • #!/usr/bin/python — Execute using Python
  • #!/usr/bin/php — Execute using PHP's command line interpreter

As Olli mentioned in his comments, sometimes env (short for environment) is used to make sure the correct path to the interpreter is chosen.

share|improve this answer
4  
Actually for non-standard things (for example perl, python and php), it's better idea to use env: #!/usr/bin/env perl, for example. That'll automatically choose correct path for perl, even if it's not /usr/bin. –  Olli Feb 23 '11 at 11:42
    
@Olli it's just some examples! ;) But you're right of course... –  BloodPhilia Feb 23 '11 at 11:43
9  
The shebang has to be the first line so the initial commented line would prevent the second line from doing anything. Another style that's often used is #!/usr/bin/env python which will usually cause the script to be run by whatever python occurs first in the PATH. This is useful since sometimes executables are installed in different places so this works without having to modify the script. In a few rare cases, env is located somewhere else, so even this fails. –  Dennis Williamson Feb 23 '11 at 11:46
1  
@Olli Happy now xD? –  BloodPhilia Feb 23 '11 at 11:48
1  
@Olli: Under the Filesystem Hierarchy Standard, "essential" programs which must be available in single-user mode must be placed in /bin, while /usr is for non-essential programs, so /bin/sh is a safer bet than /usr/bin/env - it's always possible that /usr may not be mounted, after all. I agree that /usr/bin/env perl is definitely better than /usr/bin/perl, though. –  Dave Sherohman Feb 23 '11 at 12:33

Somewhat more technical explanation:

When you run an executable on UNIX/Linux/Windows/SomeOS/AnyOS the system needs to know how to run it. In UNIX/Linux are special rules for the first few bytes of the files to tell the kernel how to run it. For scripts, the special characters are '#!'. If The kernel sees this as the first two characters, it knows the file is a script and looks at the rest of the line for the executable that runs the script. So if the file /path/to/script.sh starts:

#!/bin/sh

The kernel would see '#!', know it is a script and find and run /bin/sh and pass the script file path /path/to/script.sh as a parameter. Since all script interpreters (bash, perl, csh, python, etc) use '#' as the comment character, they'll all just ignore this 'shebang' line as a comment.

You probably never saw this before because Windows handles this completely differently. On Windows, it doesn't use these 'magic' characters, but instead maps the file extension to the way to run it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.