LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Difference while executing script as . and ./ (https://www.linuxquestions.org/questions/linux-newbie-8/difference-while-executing-script-as-and-794931/)

abhayrathod 03-12-2010 05:09 AM

Difference while executing script as . and ./
 
Hi

What is the difference when we execute a sh script like

Quote:

hostname#. script.sh
and

Quote:

hostname#./script.sh
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 (./script.sh) 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 script.sh and it will be found in the PATH and run. The ./ just means "look here, and nowhere else".

The second syntax (. script.sh - or even ". ./script.sh" 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.

Thanks

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)

Quote:

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 03:36 PM.