Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "source script.sh" and "./script.sh"?

Tags:

linux

shell

unix

What is the difference between source <script> and ./<script>?

like image 533
Arovit Avatar asked Jan 24 '11 07:01

Arovit


People also ask

What is the difference between sourcing a script vs running a script using shell?

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.

What is the difference between source and bash?

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.

What does sh mean in script?

sh extension is a scripting language commands file that contains computer program to be run by Unix shell.

What is script.sh in bash?

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.


2 Answers

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.

like image 72
Amber Avatar answered Sep 29 '22 20:09

Amber


source script will change your current environment, ./script will not.

(EDIT: script has to be executable to use ./)

like image 31
klang Avatar answered Sep 29 '22 21:09

klang