LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-03-2016, 02:23 AM   #1
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,574
Blog Entries: 19

Rep: Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453
How does a "hashbang" line actually work?


I'm familiar with the fact that when you execute a script, the program that the kernel actually launches is the interpreter program named on the #! line, and the script is then passed to that program as an argument. But what program actually carries out the necessary analysis? Does the kernel say, "Oops! This is text, not machine code; I need to look for an interpreter." Or does the shell do something different when it forks off the process? That seems more likely, but not all scripts are launched from a shell.

AntiX has a rather neat graphical Control Centre for administrative tasks which is written in Python. It uses pygtk to create a nice dialog window with tabs and big square buttons. When you press a button, a shell script is launched to carry out the specified task. Here fluxbox must be forking the python script which then forks the shell script.

All the code is in /usr/local/bin (which is where AntiX stores stuff that doesn't come from Debian) so you can inspect it and see how it works on the surface. But it made me wonder how a thing like that works internally.
 
Old 09-03-2016, 02:28 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
This satisfied my curiosity:

https://en.wikipedia.org/wiki/Interpreter_directive
 
1 members found this post helpful.
Old 09-03-2016, 02:53 AM   #3
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,574

Original Poster
Blog Entries: 19

Rep: Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453Reputation: 4453
Quote:
Originally Posted by dugan View Post
Actually that entry doesn't answer the question of how it works internally. But the entry for shebang which it links to does. Apparently it's the exec() function that carries out the necessary analysis.
Quote:
Originally Posted by Wikipedia
The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being 0x23 0x21, the two-character encoding in ASCII of #!. This magic number is detected by the "exec" family of functions, which determine whether a file is a script or an executable binary. The presence of the shebang will result in the execution of the specified executable, usually an interpreter for the script's language.
 
1 members found this post helpful.
  


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
"Line 7 : syntax error near unexpected token ')' and Line 7: "option 1-?")" tiffney2018 Linux - Newbie 5 05-07-2016 03:35 PM
[SOLVED] sql: put "beginning of line" and "end of line" within a charset... masavini Programming 7 09-19-2012 08:03 AM
[SOLVED] "while read line" doesn't work with ssh in the loop body asemaany Programming 5 03-28-2010 01:14 AM
LXer: On-line applications "just work"; why worry about the freedom of the licence? LXer Syndicated Linux News 0 10-02-2008 09:30 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM

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

All times are GMT -5. The time now is 09:52 PM.

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