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 run the script in the current shell, executing will create a new shell and run the script there. Use source if you want to have your environment changed, use execute otherwise.
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. The check afterwards show that the variable is not set and the directory not changed. What happened? The changes were made in a new shell. The current shell spawned a new shell to run the script. The script is running 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 run 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
(the special variable
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 run 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.
If I recall correct, executing the script runs the executable in the
In addition to above,
executing the script as