Difference while executing script as . and ./
Hi
What is the difference when we execute a sh script like Quote:
Quote:
Thanks in advance |
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. |
Thanks for the answer. My concern was also about env variables not setting from script.
Thanks |
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:
|
All times are GMT -5. The time now is 03:36 PM. |