-   Linux - Newbie (
-   -   Difference while executing script as . and ./ (

abhayrathod 03-12-2010 05:09 AM

Difference while executing script as . and ./

What is the difference when we execute a sh script like




Can any one please explain the difference?

Thanks in advance

blacky_5251 03-12-2010 05:29 AM

Let me address them in reverse order. The second syntax (./ tells your shell to look in the current directory for the script, and run it in a sub-shell. Any variables the script sets or changes are not available to the parent shell. This syntax is only needed if the current directory is not in the PATH. Otherwise you can just say and it will be found in the PATH and run. The ./ just means "look here, and nowhere else".

The second syntax (. - or even ". ./" if the script is in your current directory) is called "sourced". The script is run in the current shell, rather than a sub-shell and the variables it changes or creates are visible to the parent shell when the script terminates. I probably don't understand the full nuances of this approach, but I use it to set or change variables so they are available in the parent shell.

abhayrathod 03-12-2010 05:59 AM

Thanks for the answer. My concern was also about env variables not setting from script.


pixellany 03-12-2010 07:27 AM

I have seen "sourcing" most often as part of scripts. In a script, ". /path/somescript" means "make somescript part of the current script" (which, by definition, means run it in the same shell)


My concern was also about env variables not setting from script.
OK, what's the question?

All times are GMT -5. The time now is 01:46 AM.