What is the difference between `./script.sh` and `sh script.sh` when executing a shell script in Linux?
When executing a shell script in Linux, there is a key difference between using `./script.sh` and `sh script.sh`.
Using `./script.sh` directly executes the script using the specified interpreter in the shebang line (#!/bin/bash), assuming the script has the executable permission set. This method ensures that the script is executed with the correct interpreter without having to explicitly specify it.
On the other hand, using `sh script.sh` invokes the 'sh' shell explicitly to run the script, regardless of the interpreter specified in the shebang line. This can lead to potential compatibility issues if the script expects specific features or behavior of another shell, such as bash.
Therefore, it is generally recommended to use `./script.sh` to execute shell scripts in Linux to ensure proper interpreter usage and avoid potential compatibility issues.
Please login or Register to submit your answer