What is the difference between source <script>
and ./<script>
?
In short: Sourcing a script runs the commands in the current shell process. Changes to the environment take effect in the current shell, too. Executing a script runs the commands in a subshell process.
source operates in the current shell and can affect variables in the current shell. bash invokes a new shell and that shell cannot affect the environment of its parent. Also, the . sh extension is completely optional and is specifically not used in shell initialization files.
sh extension is a scripting language commands file that contains computer program to be run by Unix shell.
A bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line. For example, you can navigate to a certain path, create a folder and spawn a process inside it using the command line.
source script.sh
runs the script within the current process, thus all variable assignments are preserved as variables even after the script finishes (and don't have to be explicitly export
'd).
./script.sh
just runs the script in a subprocess, and any variables which are assigned disappear after the script is done.
source script will change your current environment, ./script will not.
(EDIT: script has to be executable to use ./)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With