diffrence in executing a shell script

hi

Could some one let me know what is the diffrence in executing a shell script as below

$sh script.sh (this is executed in a subshell)
$./script.sh(this is executed in the current shell)
$script.sh(this is executed in a subshell)

where script.sh is the name of the shell script.

Iam not clear how they are diffrent. Please explain in detail.

Thank you.

There is no difference in how any of these are executed in that they all fork new shells.

cat Test
echo $$


echo $$; ./Test
13426784
12693590

The first one will run the script using sh, overriding what the shebang (1st line of your script says (i.e. #!/usr/bin/ksh)).

cat Test
#!/usr/bin/ksh
pushd

./Test
./Test[2]: pushd:  not found.

> bash ./Test
./Test: line 2: pushd: no other directory

It will run the script whether it's executable or not.

The second one will run the script from the current directory (./script.sh), overriding any other script.sh which is in the path.

The last one will run the first script.sh it finds in the path, not the current directory.

> ls -l Test
-rwx------    2 me     mygroup             21 Oct 19 10:15 T

> Test
ksh: Test: not found.

> PATH=.:$PATH

> Test
./Test[2]: pushd:  not found.

You missed one:

. ./script.sh

This will run script.sh in the current shell:

> cat Test
echo $$

> echo $$; . ./Test
13426784
13426784