Source command usage:
Source filename
Purpose: read and execute the command in filename in the current bash environment.
Note:This command is usually replaced by the "." command.
For example, Source
. Bash_rc is equivalent to... bash_rc.
The source command (from the C shell) is Bash
Shell built-in commands. The dot command is a dot symbol (From Bourne
Is another name of source. Similarly, the variables configured in the current script will also be used as the script environment. The source (or point) command is usually used to re-execute the modified initialization document, as shown in
. Bash_profile and. profile. For example
If the term variable is modified, you can use the source command to re-Execute. bash_profile
You do not need to log out and log on again.
For example, in a script, export $ KKK = 111
If you use./A. Sh to execute the script, after the script is executed, you run echo $ KKK and no value is found. If you use source to execute the script, then Echo
Kkk = 111. Because the./A. Sh command is called to run the shell in a sub-shell, the structure is not reflected in the parent shell after execution,
Different from source, it is executed in this shell, so we can see the result.
Small Test
1. Create test. Sh
#! /Bin/bash
Export S =/home/JBoss/
2
Run the following command: source test. Sh.
Echo $ s
Result output:/home/JBoss/
3
New Shell
Run the following command:
./Test. Sh
Echo $ s
Result:
No output S Value
Conclusion:
1. The script is run in a sub-shell environment. After the script is executed, the sub-shell automatically exits.
2. system environment variables in a shell will be copied to the sub-shell (variables defined using export );
3. The system environment variables in a shell are only valid for the shell or its sub-shell. When the shell ends, the variables disappear (and cannot be returned to the parent shell ).
4. Variables not defined by export are only valid for this shell and are also invalid for the sub-shell.
5. directly run a script file in a sub-shell, while source runs in the Current Shell environment.
source allows scripts to affect their parent shell environment, which is the opposite of export affecting the subshell environment.