You prefix sh when you want a specific shell to be used to interpret the script and not the current shell of the current user, no matter if interactive or as crontab entry.
Thank You Cero.
So, If I use shebang ( #!/bin/bash as the first line) in the script itself, I don't need to place sh before the script name to execute it.Right ?
Both are valid ways of doing it. Look at a (properly written) scripts beginning and you will usually see:
#! /path/to/interpreter
... <rest of the script ...
This requests the system to first load /path/to/interpreter and then feed it the rest of the script as input. If your first line is #! /path/to/bash this interpreter will be bash and hence your script will be executed as bash-shellscript.
Every shell (make that: every shell i know of) also has the ability to accept a parameter on the command line, which denotes a list of commands which wll be in turn executed by the shell you have called:
/path/to/bash /some/file
will load /path/to/bash into memory and then - instead of initiating an interactive session - execute what is in /some/file one line after the other. (In principle there is no difference between scripts and commands entered at the commandline, a "script" is in fact just a list of commands in a file.)
is that in the second variant /some/script.file has to have the executable-flag set in order to be executed:
# ls -l
total 8
-rw-r--r-- 1 bakunin users 86 Mar 28 15:49 will.not.execute
-rwxr-xr-- 1 bakunin users 86 Mar 28 15:49 will.execute
in the first variant it is not necessary to have the x-flag set and in the above this:
# /path/to/shell /some/path/will.not.execute
will be processed as well. Therefore using this in crontab will allow you to execute something as script without having its x-flag set.
I leave it to you to decide if this is a desirable state of affairs. Personally i think it is not, but then, i have a certain way of doing things and you might come to different conclusions. There are more than one ways to skin a cat.
Basically there is no need to prefix the script with a shell - if you leave it out, your current shell will be used. But different shells use different syntax. So naming which shell to use ensures that the script is interpreted by the shell it was written for.
A shebang in a script does more or less the same what "prefixing" does: invoce the shell with the scriptname (+ arguments) as argument, so prefixing is not needed.