LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2006, 11:12 AM   #1
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Rep: Reputation: 15
How does #! work?


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?
 
Old 05-09-2006, 11:46 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
It just passes it and all the parameters to it as parameters.
 
Old 05-09-2006, 11:46 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
I have always been led to belive that the #! line tells the shell what executable to use to process the file.

Therefore having #!/usr/bin/vi
as the first line in an executable file is like running:
vi {filename} from the prompt.

Note, the #! {executable} must be the first line in the file to work.
 
Old 05-09-2006, 11:57 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Not shell just kernel. Right in the rest.
 
Old 05-09-2006, 11:59 AM   #5
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
The above post is the correct answer. semi-quoted from Learning Perl:
Quote:
The line with #! at the top of the program informs the shell where the perl executable is located
 
Old 05-09-2006, 12:05 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This is slightly incorrect, "#!" is not a shell thing but a kernel executable loader (ld.so/ld-linux.so) one.

The interpreter along with optional parameters and the filename itself is executed.

Last edited by jlliagre; 05-09-2006 at 12:07 PM.
 
Old 05-09-2006, 07:24 PM   #7
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
so it works like...

Quote:
Originally Posted by jlliagre
This is slightly incorrect, "#!" is not a shell thing but a kernel executable loader (ld.so/ld-linux.so) one.

The interpreter along with optional parameters and the filename itself is executed.
So the name of the file is passed as a parameter

./jim.txt<cr>... (jim.txt is executable)
=============================================================
#!/bin/vi

=============================================================
argv = "vi","jim.txt" or somthing like that?
 
Old 05-09-2006, 09:05 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Note that a binary executable only needs 'x' permission(s), whereas a (text file) script using this technique needs 'rx' permissions(s).
 
Old 05-09-2006, 10:06 PM   #9
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.

... Did that make any sense?
 
Old 05-09-2006, 11:18 PM   #10
pentalive
Member
 
Registered: Jun 2005
Location: Sacramento, CA
Distribution: Many, Old and New
Posts: 124

Original Poster
Rep: Reputation: 15
It made some sense.. I'll probably write a simple C program that just prints out all
of it's arguments (like echo...)
 
Old 05-10-2006, 12:11 AM   #11
Intimidator
Member
 
Registered: Mar 2005
Distribution: FC4
Posts: 83

Rep: Reputation: 15
Quote:
Originally Posted by chrism01
Note that a binary executable only needs 'x' permission(s), whereas a (text file) script using this technique needs 'rx' permissions(s).
Can we execute a file(Just +x permission) without read permissions!?
 
Old 05-10-2006, 01:02 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 05-10-2006, 01:36 AM   #13
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
stupid #! tricks:
Code:
#! /bin/sed 1d
hello world
Code:
#! /bin/rm
this message will self destruct in some secondes
Code:
#! /usr/bin/make -f
hello:
        (echo '#include <stdio.h>'; \
         echo 'int main(){printf("hello world\n");}') \
        |gcc -x c -o $@ -
Code:
#! /usr/bin/lpr
Hello printer
 
Old 05-10-2006, 09:37 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NFS doesn't work work well with mounted NTFS volumes? StevenO Linux - Networking 6 02-13-2006 10:54 AM
Does the mounting of a *BSD partition on linux work work well when rw ? kloss *BSD 3 10-08-2005 02:21 PM
Will Ubuntu Work work With my Wireless Card? PulsarSL Ubuntu 3 10-07-2005 10:33 PM
got ALSA to work in root, need help getting it to work in my user profile ic3 Slackware 2 11-28-2003 10:32 PM
FALCONS EYE (nethack) FAILS ON STARTUP, use to work, wont work even after reinstall roorings Linux - Software 0 10-08-2003 10:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration