Shebang


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shebang
# 1  
Old 04-29-2011
Shebang

If i am not using #! in my script. By default where will be my script running?
# 2  
Old 04-29-2011
It will be run by the shell under which you are when you run it.
# 3  
Old 04-29-2011
current shell = echo $SHELL this o/p right. please advice if i am running in ksh what will be the difference from sh ( kron /bash).
# 4  
Old 04-29-2011
All depends on what is in the code of your script

If your script has been coded so that it follow the POSIX standard, i suppose ksh and bash could be able to handle it.

BUT if you use some specific ksh syntax or specific bash syntax in your code (by specific, i mean, some featured syntax that are NOT understood by another shell) then, if you don't specify which shell should run your script in the shebang, then it will lead to error and/or unexpected behaviour if run by an inadequate shell.
# 5  
Old 04-29-2011
Quote:
Originally Posted by Kochu77
current shell = echo $SHELL this o/p right. please advice if i am running in ksh what will be the difference from sh ( kron /bash).
No, current shell = echo $0
Code:
$ echo $SHELL
/bin/ksh
$ echo $0
-ksh
$ bash
bash-2.05$ echo $0
bash
bash-2.05$ echo $SHELL
/bin/ksh

# 6  
Old 04-29-2011
Yes, by "current shell" i meant $0 as vgersh99 pointed it out to you, not $SHELL !!!
# 7  
Old 04-29-2011
Code:
$ ps -o comm -p $$ | sed 1d
ksh
$
$ cat ./fubar
:
echo shell = $0
pid=$$

comm=`ps -o comm -p $pid | sed 1d`
echo comm = $comm
cmd=`ps -o cmd -p $pid | sed 1d`
echo cmd = $cmd
args=`ps -o args -p $pid | sed 1d`
echo args = $args
$
$
$ ./fubar
shell = ./fubar
comm = ksh
cmd = ./fubar
args = ./fubar
$
$
$ csh
$
$  ps -o comm -p $$ | sed 1d
csh
$ ./fubar
shell = ./fubar
comm = sh
cmd = /bin/sh ./fubar
args = /bin/sh ./fubar
$

SHELL is used by some programs to understand what shell you would like them to use. I used to use csh as my interactive shell but set SHELL to /bin/sh so that "make" would work well. (This was before bash or ksh.)

Note that csh decided to run fubar in /bin/sh. A better rule is that without a shebang your interactive shell will select whatever shell it feels like selecting to run your script. I believe that without the leading : csh might select itself but I don't feel like testing it. That fubar script should work correctly in any shell I know of, so it might be ok to let it run in any shell.

I think that asking the ps program is probably the best way to find out what shell is currently running. It's what I do anyway.
This User Gave Thanks to Perderabo For This Post:
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

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

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: dips_ag
7 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