[CentOS] Real sh? Or other efficient shell for non-interactive scripts

Fri Apr 24 17:36:59 UTC 2015
Valeri Galtsev <galtsev at kicp.uchicago.edu>

On Fri, April 24, 2015 12:04 pm, John R Pierce wrote:
> On 4/24/2015 9:47 AM, Gordon Messmer wrote:
>> On 04/24/2015 03:57 AM, Pete Geenhuizen wrote:
>>> if you leave it out the script will run in whatever environment it
>>> currently is in.
>> I'm reasonably certain that a script with no shebang will run with
>> /bin/sh.  I interpret your statement to mean that if a user is using
>> ksh and enters the path to such a script, it would also run in ksh.
>> That would only be true if you "sourced" the script from your shell.
> oh fun, just did some tests (using c6.latest).   if you're in bash,
> ./script (sans shebang) runs it in bash.  if you're in dash or csh,
> ./script runs it in sh.    if you're in ksh, it runs it in ksh.

Wow! Surprise ;-)

I just tested it on my FreeBSD workstation, and all works as expected
(i.e. the script obeys shebang). Just in case, here is the contents of my
test script:


readlink /proc/$$/file

( note that that "file" is because I'm using FreeBSD /proc, for Linux you
may need to replace the line with something like:

readlink /proc/$$/exe

Now the fun part

in bash:

$ echo $0

$ ./test

in tcsh

% echo $0

% ./test

in zsh

% echo $0

% ./test

But yet funnier thing:

$ bash ./test

$ tcsh ./test

$ zsh ./test

Well, no creepy surprises for me ! ;-)

(you can do the same on Linux of your choice and see if it behaves ;-)


Valeri Galtsev
Sr System Administrator
Department of Astronomy and Astrophysics
Kavli Institute for Cosmological Physics
University of Chicago
Phone: 773-702-4247