What is the difference between executing a bash script like A and sourcing a bash script like B?
A >./myscript B >source myscript
|show 1 more comment|
This question came from our site for professional and enthusiast programmers.
Short answer: sourcing will execute the commands in the current shell, executing will create a new shell and execute the commands there.
Before we execute the script first we check the current environment:
Now we execute the file:
Check the environment again:
The script output clearly shows that the variable was set and the directory was changed. Where did the changes go? The changes were made in a new shell. The current shell spawned a new shell to execute the script. The script is executing in the new shell and all changes to the environment take effect in the new shell. After the script is done the new shell is destroyed. All changes to the environment in the new shell are destroyed with the new shell. Only the output text is printed in the current shell.
Now we source the file:
Check the environment again:
The variable FOO is set and the working directory has changed.
Sourcing the script does not create a new shell. All commands are executed in the current shell and changes to the environment take effect in the current shell.
Note that in this simple example the output of executing is the same as sourcing the script. This is not necessarily always the case.
Consider following script
First print the pid of the current shell:
Source the script:
Execute the script, note the PID:
You can see that sourcing the script runs in the same process while executing the script creates a new process everytime. That new process is the new shell which was created for the execution of the script. Sourcing the script does not create a new shell and thus the PID stays the same.
Both sourcing and executing the script will process the commands in the script line by line, as if you typed those commands by hand line by line.
The differences are:
Executing a script runs it in a separate child process, i.e., a separate instance of shell is invoked to process the script. This means that any environment variables etc., defined in the script can't be updated in the parent (current) shell.
Sourcing a script means that it is parsed and executed by the current shell itself. It's as if you typed the contents of the script. For this reason, the script being sourced need not be executable. But it has to be executable if you're executing it of course.
If you have positional arguments in the current shell, they're unchanged.
So if I have a file
and I do:
I get something like:
Hope that helps.
Sourcing you get all the extra variables defined in the script.
sourcing is essentially the same as typing each line of the script in at the command prompt one at a time...
Execution starts a new process and then runs each line of the script, only modifying the current environment by what it returns.
In addition to above,
executing the script as
If I recall correct, executing the script runs the executable in the