What does "#! /usr/bin/ksh" mean? | Unix Linux Forums | Answers to Frequently Asked Questions

  Go Back    


Answers to Frequently Asked Questions FAQ for UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

What does "#! /usr/bin/ksh" mean?

Answers to Frequently Asked Questions


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2002
Nisha Nisha is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 16 August 2004, 4:51 AM EDT
Location: Chennai, India
Posts: 110
Thanks: 0
Thanked 1 Time in 1 Post
Tools What does "#! /usr/bin/ksh" mean?

Whenever we write a shell script the first statement would be #! /usr/bin/ksh or something of the same..

What does this signify??? I know # is a comment.. what is this ! for???

Give in your thoughts....


Thanks,
Nisha
Sponsored Links
    #2  
Old 07-12-2002
hell666 hell666 is offline
Registered User
 
Join Date: Jul 2002
Last Activity: 16 November 2002, 6:32 AM EST
Location: void
Posts: 53
Thanks: 0
Thanked 1 Time in 1 Post
It's to tell your shell what shell to you in executing the following statements in your shell script.
The Following User Says Thank You to hell666 For This Useful Post:
jameson (05-22-2012)
Sponsored Links
    #3  
Old 07-12-2002
Nisha Nisha is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 16 August 2004, 4:51 AM EDT
Location: Chennai, India
Posts: 110
Thanks: 0
Thanked 1 Time in 1 Post
Question

is that all hell666??????
    #4  
Old 07-12-2002
hell666 hell666 is offline
Registered User
 
Join Date: Jul 2002
Last Activity: 16 November 2002, 6:32 AM EST
Location: void
Posts: 53
Thanks: 0
Thanked 1 Time in 1 Post
Yes, that is it. But, this kind of comment should be in your first line of the script, only then does it take this form. Also, when your shell comes across this line, a new process is spawned and replaced with this shell of your choice, and it is this process which will parse your script.
Sponsored Links
    #5  
Old 07-12-2002
killerserv's Avatar
killerserv killerserv is offline Forum Advisor  
Registered User
 
Join Date: Dec 2000
Last Activity: 9 August 2014, 10:30 PM EDT
Location: Phoenix, AZ
Posts: 670
Thanks: 0
Thanked 3 Times in 3 Posts
On the first line of an interpreter script, the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the contents of the file are executed as a shell script.

You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. The '!' is also known as intepreter for the script to recognise correct compiler to be used.
Sponsored Links
    #6  
Old 07-13-2002
Nisha Nisha is offline
Registered User
 
Join Date: Jun 2002
Last Activity: 16 August 2004, 4:51 AM EDT
Location: Chennai, India
Posts: 110
Thanks: 0
Thanked 1 Time in 1 Post
Computer

Thank you Killerserv.


-Nisha
Sponsored Links
    #7  
Old 07-13-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 20 October 2014, 3:15 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
This will probably be more than you wanted to know, but here goes anyway....

Originally, we only had one shell on unix. When you asked to run a command, the shell would attempt to invoke one of the exec() system calls on it. It the command was an executable, the exec would succeed and the command would run. If the exec() failed, the shell would not give up, instead it would try to interpet the command file as if it were a shell script.

Then unix got more shells and the situation became confused. Most folks would write scripts in one shell and type commands in another. And each shell had differing rules for feeding scripts to an interpreter.

This is when the "#! /" trick was invented. The idea was to let the kernel's exec() system calls succeed with shell scripts. When the kernel tries to exec() a file, it looks at the first 4 bytes which represent an integer called a magic number. This tells the kernel if it should try to run the file or not. So "#! /" was added to magic numbers that the kernel knows and it was extended to actually be able to run shell scripts by itself. But some people could not type "#! /", they kept leaving the space out. So the kernel was exended a bit again to allow "#!/" to work as a special 3 byte magic number.
So
#! /usr/bin/ksh
and
#!/usr/bin/ksh
now mean the same thing. I always use the former since at least some kernels might still exist that don't understand the latter.

And note that the first line is a signal to the kernel, and not to the shell. What happens now is that when shells try to run scripts via exec() they just succeed. And we never stumble on their various fallback schemes.
The Following 11 Users Say Thank You to Perderabo For This Useful Post:
chacko193 (04-23-2014), grep_me (03-22-2011), jameson (05-22-2012), kunwar (12-04-2010), mathbalaji (06-25-2012), NeduvaR (01-03-2011), raderb (02-03-2012), saps19 (07-26-2011), skippyV (09-15-2011), swatt (12-26-2010), WSUToad (06-25-2010)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Make scipt except from "Y","y" and "yes" to take [Enter] as being "yes" hakermania Shell Programming and Scripting 4 12-11-2010 09:24 AM
ps -ef | grep "string1" "string2" " "string3" steve2216 Shell Programming and Scripting 11 09-03-2010 11:08 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM



All times are GMT -4. The time now is 12:48 AM.