LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   benefit of source command (https://www.linuxquestions.org/questions/linux-newbie-8/benefit-of-source-command-4175533611/)

Upendra Pratap Singh 02-10-2015 10:24 AM

benefit of source command
 
Hi,

What are the benefits that we get when we choose to source a file rather than execute it as a shell script?

and then in what way is export opposite to source?

It looks like they are doing the same thing, i.e making environment variables accessible in the local shell

rtmistler 02-10-2015 11:59 AM

Executing it as a shell script is using the source command because source is abbreviated with the DOT. It's really that simple, DOT is source, and vice-versa.

http://en.wikipedia.org/wiki/Source_%28command%29

Or see the first answer, not the comments, the answer http://superuser.com/questions/46139...does-source-do where they say:
Quote:

source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in '.' (period).


jpollard 02-10-2015 02:56 PM

Quote:

Originally Posted by Upendra Pratap Singh (Post 5315036)
Hi,

What are the benefits that we get when we choose to source a file rather than execute it as a shell script?

Primarily, this is used so that modifications to the environment (such as PATH) that are made in the script will be usable by the shell in the future.
Quote:

and then in what way is export opposite to source?
One problem with including scripts this way is that the shell environment starts getting lots of variables that don't make any sense. This is the advantage a separate process has - its environment is isolated from the parent (initially copied, but any changes made will not affect the parent), and the environment is automatically cleaned up when the process exits.
Quote:


It looks like they are doing the same thing, i.e making environment variables accessible in the local shell
Only when the script is included will the variables be available to the shell. When run as a new process, it is separate.


All times are GMT -5. The time now is 06:42 PM.