What is the difference between both the commands?
sh script.sh
tells the sh
program to run the script.
./script.sh
says to run the script using the program that the script defines with a hashbang at the start (or in the default shell if not hashbang).
Note that you may well end up with completely different results, depending on whether the script has a hashbang, for a couple of reasons:
Even when sh
is bash
, when it's invoked as sh
it runs in a limited compatibility mode (thank you Charles Duffy for pointing that out in the comments on the question).
The script might run with something completely different from a shell. It might start with #!/bin/node
and contain JavaScript code to run in Node.js. In that case, ./script.sh
will work but sh script.sh
will fail, because sh
will treat the script as a shell script.
Why it's not required change in permission for 'sh script.sh'?
Because in that case, what you're executing is the sh
program, which is marked executable (it's sh
that then reads and performs the commands in the script, but the script doesn't have to be executable for that). When you do ./script.sh
, you're treating it as executable, so it needs to have the executable permissions bit set.