The Shebang!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting The Shebang!
# 1  
Old 05-07-2010
The Shebang!

Hi,

I always thought that #!/usr/bin/ksh means that the script would be executed in korn shell i.e. when we'll execute the script with this line as the very first line then the shell spawns a korn shell (in this case as we are using #!/usr/bin/ksh ) and the script gets executed.

But I am not sure if my understanding is correct because I tried to execute a script with #!/usr/bin/ksh
as below:
Code:
sh a_korn_shell_script.ksh

from a GUI tool. The script failed! with the following error
Code:
 
a_korn_shell_script.ksh: line 14: syntax error in conditional expression: unexpected token `('
a_korn_shell_script.ksh: line 14: syntax error near `+(['
a_korn_shell_script.ksh: line 14: `if [[ $var = +([0-9]) ]]'

The snippet of the code which was giving problem
Code:
 
if [[ $var = +([0-9]) ]];then
echo "number"
else
echo "not number"
fi

But when I executed it as below:
Code:
ksh a_korn_shell_script.ksh (NOTE: This is through GUI tool) OR ./a_korn_shell_script.ksh (NOTE: This is in unix box)

it succeeded.
Code:
+ var=0
+ echo number
number

I came accross this link: https://www.unix.com/shell-programmin...sh-script.html
which means that even if we use #!/usr/bin/ksh line we should not feed a korn shell script to sh shell??

The default shell is
Code:
 
echo $SHELL
/bin/bash

Please execuse me if you think my question is very basic.
-dips
# 2  
Old 05-07-2010
Because you execute your script this way

Code:
sh a_korn_shell_script.ksh

your script is actually run under the Bourne shell - not Korn. The first line is treated like a comment since it starts with a "#".

Try running your script without the "sh" and see what happens.
# 3  
Old 05-07-2010
Quote:
Originally Posted by dips_ag
Code:
sh a_korn_shell_script.ksh

from a GUI tool. The script failed!
You're launching a_korn_shell_script.ksh with sh shell. In that case, shebang is ignored.
# 4  
Old 05-07-2010
You are asking your script to execute as a bourne script by prefacing it with "sh".

For kicks and giggles, try the following:
Remove the "#!/blah/blah" from the first line of your script. Then try to run the script with the following command:
Code:
ksh a_korn_shell_script.ksh

You will see that the line indicating the "intended" shell, is overridden by telling it explicitly which interpreter to use.
# 5  
Old 05-08-2010
Quote:
For kicks and giggles, try the following:
Remove the "#!/blah/blah" from the first line of your script. Then try to run the script with the following command:
Code:
Code:
ksh a_korn_shell_script.ksh

I did that and yes the script executed fine with no errors.

Quote:
The first line is treated like a comment since it starts with a "#".
If this is a comment then why we need it? Is it just to tell others that a particular script is intended for which shell?
Please be patient with me and explain this Smilie

-dips
# 6  
Old 05-08-2010
Actually, you can execute a script without sh, ksh, bash, perl, ... in front of the name on the said script.
If so, the shebang is used to determine which program to call to execute the script.
In that case, you have to make your script executable with something like chmod +x /path/to/your_script
Hope it's clear enough. Others members will surely add more comments and explanations.
# 7  
Old 05-08-2010
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripts without shebang

I see lot of ad-hoc shell scripts in our servers which don't have a shebang at the beginning . Does this mean that it will run on any shell ? Is it a good practice to create scripts (even ad-hoc ones) without shebang ? (16 Replies)
Discussion started by: kraljic
16 Replies

2. Shell Programming and Scripting

Necessity of shebang line

Hi , I know about the shebang line in shell scripting. Just want to know whether is there any difference in execution of the program by keeping and not keeping the shebang line. Because without shebang line also the script is working. correct me if am wrong. Any help on this will be helpful (5 Replies)
Discussion started by: rogerben
5 Replies

3. Shell Programming and Scripting

csh shebang query

What does the "-f" mean in following interpreter code #!/bin/csh -f Thank you (2 Replies)
Discussion started by: animesharma
2 Replies

4. Shell Programming and Scripting

Shebang

If i am not using #! in my script. By default where will be my script running? (6 Replies)
Discussion started by: Kochu77
6 Replies

5. Shell Programming and Scripting

Hyphen char after shebang notation

Hi, I have a trivial question to ask, I am seeing in some shell scripts the '-' (hyphen) character following the first line of shell script (i.e) the shebang notation as follows: #!/bin/sh - #! /bin/bash - what does the hyphen signify? What will happen if it is not given explicitly? (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

Multiple shebang lines

*** EDIT: I found something close to my solution under an IIS 7 Module Handle.***** (Non-Homework question, simply an ease of use one) Odd question here and maybe its my newness to cgi/Perl, but is it possible to have 2 shebang lines? I write an test a ton of my homework code on my windows... (1 Reply)
Discussion started by: sennex
1 Replies

7. Shell Programming and Scripting

Relacing the shebang line of a file

Can any one tell me how to replace a shebang line of a file using sed? Eg: If a file contains the following shebang line #!C:/InstantRails/ruby/bin/ruby I would like to replace it with #!/usr/local/bin/ruby The shebang line of the file can be obtained from the command cat... (3 Replies)
Discussion started by: linuxnewbe
3 Replies

8. Shell Programming and Scripting

Doubt in shebang line!!

Do we need to include the exclamatory mark in the shebang line??:confused: What if we dont include it??:eek: Actually what shebang line implies when we run a script?? shebang line--> #!/bin/ksh :p (6 Replies)
Discussion started by: nohup
6 Replies

9. Shell Programming and Scripting

Shebang

Hi, I am currently writing BASH shell scripts. I am using BASH on a Powerbook G4 running Leopard. Could somebody please explain the difference between #!/bin/bash and #!/bin/sh? I have been using the latter (#!/bin/sh), and things have been working fine. But is that the correct one to use... (9 Replies)
Discussion started by: msb65
9 Replies
Login or Register to Ask a Question