Help with the #!/bin/whatever line in scripts

Forum Index » Forums » Programming and Development
Author Message
Joined: Jul 05, 2008
Posts: 2
Other Topics
Posted Jul 05, 2008 at 9:11:34 PM
Subject: Help with the #!/bin/whatever line in scripts
I'm writing a lexer for a custom scripting language, and I want it to parse the first line of the scripts the same way the shell/kernel does. I'm wondering if anyone has the EXACT specification of what is legal/illegal in that area. Or information on where it's parsed. I am specifically talking about lines like this: #!/bin/bash bash code here Or, you can specify any executable in the first line, with parameters, for example: #!/home/smlefo/test -a -b this is a custom script Which will execute the 'test' program, with the parameters -a and -b, along with the filename. I'm curious as to where this first line is parsed (is it part of the ELF specification? or is interpreted by the shell, or somewhere in the kernel?). Also, during my investigation of what is legal, I found out that the absolute path can't have any spaces... so for example: #!/home/smlefo/test directory/test -a -b blah Will obviously execute the file /home/smlefo/test, with the parameters directory/test, -a, and -b. But what if I want it to interpret "test directory" as an actual directory? I tried: #!/home/smlefo/test\ directory/test -a -b blah But that failed with the error: bash: ./ts: /home/smlefo/test\: bad interpreter: No such file or directory So... if anyone has any hints as to where I can find the exact specification of this, that would be very helpful. Thank you. ~Sean
Back to top Profile Email Website
Johannes Truschnigg

Joined Jun 15, 2008
Posts: 27

Other Topics
Posted: Jul 13, 2008 6:58:02 PM
Install and unroll a recent kernel source tree tarball, and have a look at /usr/src/linux/fs/binfmt_script.c That's where shebang and interpreter parsing apparently is done.

Free software. Free society. Better lives.

Back to top Profile Email Website
smlefo
Joined Jul 05, 2008
Posts: 2

Other Topics
Posted: Jul 17, 2008 1:24:39 AM
Sweet! Exactly what I was looking for. Thank you kind sir.
Back to top Profile Email Website
Forum Index » Forums » Programming and Development