Should I run scripts like sh /absolute_path/to/scriptname.sh or just /absolute_path/to/scriptname.sh

Shell : Linux Bash
OS version : RHEL 6.X, Oracle Linux 6.X

Question1. To execute shell scripts, sometimes I see Linux admins placing sh command before shell scripts like below

sh /home/appadmin/sync_pending_orders.sh

But, I execute shell scripts like below ie. Just the absolute path

/home/appadmin/sync_pending_orders.sh

Which is recommended ?

Question2. Similarly, in crontab entries , I see sh being placed before shell script names like below. Is this sh 'prefixing' really needed ?

[appadmin@ktsappwprg01 ~]$ crontab -l
35 2 * * * /bin/sh /home/appadmin/sync_orders.sh >> /home/appadmin/log/ind_critical.log

25 3 * * * /bin/sh /home/appadmin/sync_pending_orders.sh >> /home/appadmin/log/ind_crtical_2.log

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.

1 Like

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

The only difference between the two variants:

/path/to/shell /some/script.file
/some/script.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.

I hope this helps.

bakunin

1 Like

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.

1 Like