ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is probably a pretty basic question... how does
"#! /bin/interpreter" work?
Does Linux handle inserting the file into standard input?
Does my program have to know to open the file (is it passed the file name)
As an experiment I created a text file with #! /bin/emacs as the first line, chmod a+x and then ./file ran it.. it loaded emacs, emacs loaded the file and then waited for me to begin editing. Did emacs have special code to handle this? or did emacs just think I typed the file?
You may have to be careful / clever when using the #! for complicated interpreters. It seems to be (according to my execve man page) that syntax for #! is:
Code:
#! interpreter [arg]
where the optional argument comprises everything after the first whitespace after the interpreter. In other words,
Code:
#!/bin/sh arg1 arg2 arg3 arg4
executes /bin/sh with the single argument "arg1 arg2 arg3 arg4". This is under FreeBSD, the manual for which claims that other systems operate in the same way. Can anybody else back this up? I'm curious as to whether it's really true or not. For example, on FreeBSD 6-STABLE:
Code:
#!/usr/local/bin/python -t -v
print "python is good\n"
results in Python warning about an unknown option "-". Clearly not what I would expect if I didn't know how it is really handled. Perl, however, doesn't seem to have this trouble, probably because it actually picks each individual member of argv apart, instead of assuming it is a complete and singular argument.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by taylor_venable
You may have to be careful / clever when using the #! for complicated interpreters. It seems to be (according to my execve man page) that syntax for #! is:
Code:
#! interpreter [arg]
where the optional argument comprises everything after the first whitespace after the interpreter. In other words,
Code:
#!/bin/sh arg1 arg2 arg3 arg4
executes /bin/sh with the single argument "arg1 arg2 arg3 arg4". This is under FreeBSD, the manual for which claims that other systems operate in the same way. Can anybody else back this up? I'm curious as to whether it's really true or not.
Yes, It is the case with most if not all Unix and Unix like O/Ses.
man execve (linux):
... a script starting with a line of the form "#! interpreter [arg]".
man execve (Solaris):
An interpreter file begins with a line of the form
#! pathname [arg]
where pathname is the path of the interpreter, and arg is an optional argument.
As I said
Note that a binary executable only needs 'x' permission(s), whereas a (text file) script using this technique needs 'rx' permissions(s).
IOW, an interpreter like bash (or perl or sql ... etc) needs to be able to read the text in order to parse/run it. OTOH, a binary executable is a standalone prog (effectively runs itself (under kernel ctrl)), so it doesn't need 'r' perms.
HTH
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.