Ubuntu – Why “./” is used to run “.sh” scripts in Unix?

scripts

I am executing a .sh script today.
It is executed with prefix "./.sh,I am a bit confuse because it is also executed without ./.but why this is required to use ./

Could you please explain me that why ./ is used to run .sh scripts?

Best Answer

The ./ is needed to tell bash interpreter where the script is located. There is a fixed set of directories that bash interpreter will check to run a command.

If your script is not in one of these directories than it will not find you script unless you specify the path. The ./ is telling the bash interpreter that the script is in the current directory.

You could also run the script from any other directory by using the full path.

e.g. ./script.sh from within /path/to is the same as running /path/to/script.sh from anywhere if the script is located in /path/to.

You can run echo $PATH to get a list of all the directories the bash interpreter will check. If you move you script or place a symbolic link in one the directories you will be able to execute it just using the file name script.