Shebang

If i am not using #! in my script. By default where will be my script running?

It will be run by the shell under which you are when you run it.

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).

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.

No, current shell = echo $0

$ echo $SHELL
/bin/ksh
$ echo $0
-ksh
$ bash
bash-2.05$ echo $0
bash
bash-2.05$ echo $SHELL
/bin/ksh

Yes, by "current shell" i meant $0 as vgersh99 pointed it out to you, not $SHELL !!!

$ 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.

1 Like