Executing a shell script using sh

Platform : Solaris 10, RHEL 5.4, OEL 6

I've noticed that some of my colleagues execute scripts by typing sh before the script name

sh myscript.sh

I always execute a script by typing the script name and typing enter provided PATH variable has . (current directory) in it

myscript.sh (and then enter)

Is there any advantage in using sh ?

If you have not eXcute priviledge in the script file, then you need start the interpreter like sh and input is readed from file (arg 1).

If your input file not include needed interpreter then it's more safety to tell which interpreter need to use.

If PATH not include dot (current directory), then you need tell the path to find the command.

Ex. a.sh

((a=100+100))
chmod a+rx a.sh
a.sh   # works if your PATH include . (dot)
./a.sh  # works always, no need to check PATH

But if your command shell is not bash or ksh in previous example, you have ex. csh or dash shell, then you get error. Your shell can't run previous script - syntax error.

=>

bash a.sh   # works fine in every user shell

Or add interpereter to the 1st line and give the execute priv.

#!/usr/bin/bash
((a=100+100))

Then it works always in any user shell. No need to take care of users shell.

Some of us has idea that when you develop something, it's not executable
=> need use

someinterpreter inputfile

Or you don't like to tell that this file is shell script, ex. you have file
readme.txt
but it include something code, then you and only you know that it's include some script for some interpreter.

bash readme.txt
awk -f readme.txt
php readme.txt
sh readme.txt
# which syntax you have used in your "hidden script"
2 Likes