Use of the source command in linux
Linux source command:
Common usage: source filepath or. filepath
Function: enables the current shell to read the shell file whose path is filepath and execute all the statements in the file in sequence. It is usually used to re-execute the modified initialization file to make it take effect immediately, you do not have to log out and log on again. For example, if you modify the/etc/profile file and want it to take effect immediately, you can use the source command, such as source/etc/profile, instead of logging on again.
The source command (from the C Shell) is the built-in command of the bash shell; the dot command (.) is a dot symbol (from the Bourne Shell) is another name of the source. This can also be seen from the usage.
Differences between source filepath and sh filepath and./filepath:
When shell scripts have executable permissions, there is no difference between using sh filepath and./filepath .. /Filepath is because the current directory is not in the PATH, and all "." is used to represent the current directory.
Sh filepath re-creates a sub-shell and executes the statements in the script in the sub-shell. The sub-shell inherits the environment variables of the parent shell, but the sub-shell is newly created, the changed variable will not be brought back to the parent shell unless export is used.
Source filename simply reads the statements in the script and runs them in the current shell. No new sub-shell is created. All statements for creating and changing variables in the script are saved in the current shell.
Example:
Create A test. sh script with the following content: A = 1;
Modify the executable permission: chmod + x test. sh;
After running sh test. sh, echo $ A is displayed as null, because A = 1 is not returned to the current shell;
After you run./test. sh, the same effect is displayed;
Run source test. sh or. test. sh, And Then echo $ A. 1 is displayed, indicating that the variable A = 1 is in the current shell;