LinuxQuestions.org
Review your favorite Linux distribution.
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-04-2022, 08:56 AM   #1
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,694
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
FYI: #!"shebang!" – the powerful secret sauce of command-line scripting


Ahh, yes ... the "BASH script." A simple scripting language that was built into a popular Unix/Linux shell that was never really intended to have one. (The only shell that I know of which was 'intended to have one' was Dr. Korn's shell, ksh, which implements a very impressive (for its time ...) built-in language that very few people today actually use.)

A disturbing number of scripts still use "bash scripting," and a disturbing number of computer programmers still think that they are limited to this. They are not. Shells provide a far more powerful feature: colloquially known as "shebang." Written as: #!.

If the first line of your script begins with – for example – #!/usr/bin/perl – then the shell will silently pass control over to [Perl] to handle the remainder of the script. And the user will never know.

"Perl ... PHP ... Ruby ... pick your oyster." (Strictly speaking, any executable file can be used here.)

This concept was further enhanced by the env command, which is specifically intended to be used on a "shebang" line to add further goodness to it ... such as, among other things, "automatically discovering where [Perl] is on this machine." It also takes care of a few other things that the shell by itself doesn't do as well.

I now refer you to man env (and, man bash) for further details.

So ... keep this in mind. Most Linux distros routinely install several of these language interpreters, and most of a vendor's built-in commands and scripts use one or more of them. Therefore, you do not have to resort to "computer calisthenics" to create a perfectly-functional "command-line command." You are not limited to bash's built-in but very limited facilities. Full-featured programming environments, which were designed to be so, are at your beck and call when writing any command-line script.

(Incidentally, this is exactly the reason why many programming languages today use the # character in column-one to denote a comment line.)

Last edited by sundialsvcs; 05-04-2022 at 09:32 AM.
 
Old 05-04-2022, 11:54 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,367
Blog Entries: 3

Rep: Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768Reputation: 3768
Quote:
Originally Posted by sundialsvcs View Post
"Perl ... PHP ... Ruby ... pick your oyster." (Strictly speaking, any executable file can be used here.)
Some more which I have made use of are GNUplot, AWK, and R

Code:
$ grep -h -m 1 -E '^#' * | sort -u

#!/usr/bin/awk -f
#!/usr/bin/gnuplot
#!/usr/bin/Rscript
As you note, the locations can vary, especially with *BSD. But for the most part on GNU/Linux distros generally keep the scripting languages in the expected place.
 
Old 05-04-2022, 01:36 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by sundialsvcs View Post
Shells provide a far more powerful feature: colloquially known as "shebang." Written as: #!.
That is actually a kernel feature. The shell isn't involved at all unless, of course, the specified interpreter happens to be a shell.
 
Old 05-04-2022, 03:29 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,793

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
Exactly, when the script is set as executable the program loader parses and executes the specified program i.e bash, php, python etc.

The shebang (#!) is actually a magic number. Magic numbers are basically what tells the system, applications whatever the type of file versus its extension like Windows.
 
Old 05-04-2022, 06:37 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,694

Original Poster
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
Quote:
Originally Posted by michaelk View Post
The shebang (#!) is actually a magic number. Magic numbers are basically what tells the system, applications whatever the type of file versus its extension like Windows.
Yes, and the file command can do that interpretation for you.

(Although, as is to be expected, Windows actually does not rely upon the file-extension alone. All files have predictable binary headers.)

Last edited by sundialsvcs; 05-04-2022 at 06:38 PM.
 
  


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
FYI: Getting to know "#!shebang" sundialsvcs Programming 12 12-24-2016 02:05 AM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM
LXer: Cluster Interconnects: The Whole Shebang LXer Syndicated Linux News 0 04-21-2006 03:21 PM
Konqueror 'magic'/'shebang' lines. richardh1970 Linux - Software 0 11-15-2005 06:57 AM
Linux won't recognize shebang line tjanzer Linux - General 2 03-15-2003 10:33 AM

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

All times are GMT -5. The time now is 01:26 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