What exactly interpret #!/bin/bash line?

6

Many scripts in different languages have a #!/bin/bash header with a path to interpreter, so they can be executed without explicit call to interpreter from command line.

But what exactly reads this line and run the interpreter, is it shell or kernel?

vava

Posted 2010-03-09T01:46:12.370

Reputation: 5 238

Answers

7

At least in Linux,the kernel has this functionality: fs/binfmt_script.c specifically.

http://www.netmite.com/android/mydroid/cupcake/kernel/fs/binfmt_script.c

I imagine, however, that some shells may bypass this, and check the first line for an interpreter line rather than just calling exec and letting the kernel figure it out.

bdk

Posted 2010-03-09T01:46:12.370

Reputation: 831

O.K. I think I miss understood question. – Maciek Sawicki – 2010-03-09T02:19:40.847

OK, after reading justin answerer I think my whole concept understanding was wrong – Maciek Sawicki – 2010-03-09T02:27:54.347

Now, I think It would be hard to bypass this. – Maciek Sawicki – 2010-03-09T02:28:34.923

It used to be the shell. http://www.in-ulm.de/~mascheck/various/shebang/ has lots of information and history.

– user1686 – 2010-03-09T21:25:36.120

Also, it is not a very good idea to link to Android source code when referring to Linux. – user1686 – 2010-03-09T21:27:43.840

1Actually, it wasn't Android specific source code, it was the Linux kernel source code as hosted on an Android site. I didn't want to provide a URL for a tarball of the kernel that would need to be untarred to get at the file. However, linking to that site turned out to not be a very good idea for a different, reason, it no longer seems to be up... At any rate, googling Linux binfmt_script.c will find other copies for anyone interested – bdk – 2010-03-09T22:28:54.650

5

The kernel reads it. It uses the #! to detect that the file is a script and not a binary, and execs the command that follows.

http://en.wikipedia.org/wiki/Shebang_(Unix)#As_magic_number explains how it works pretty well.

user23307

Posted 2010-03-09T01:46:12.370

Reputation: 5 915