Thursday, March 17, 2011

Why ./ is used to run shell scripts?

By default, UNIX or Linux based operating systems will search the executable of any command under the directories mentioned in the PATH variable to run the command.

If location of the shell script is not included in the PATH variable. Then we need to run the shell script by explicitly mentioning the directory name. This can be achieved in two ways.


a) Explicitly providing the absolute path(directory name from /) of the script
eg:
volcano@volcano-laptop:~/shellscript$ /home/volcano/shellscript/hello.sh
Hello World!

b) Explicitly providing the relative path (current directory) of the script
eg:
volcano@volcano-laptop:~/shellscript$ ./hello.sh
Hello World!

Meaning of ./ ( dot slash):
. -> Always refers to current directory
/ -> directory path separator

If you are not providing the directory separator means it will look like hidden file execution(.hello.sh). Then, operating system will try to run this as hidden file and then it will fail.So slash (/) is must after the . (dot) command to run the script.

No comments:

Post a Comment