Home > Operation and Maintenance > Linux Operation and Maintenance > What is the use of linux source command?

What is the use of linux source command?

藏色散人
Release: 2023-01-05 16:13:52
Original
13698 people have browsed it

The function of the linux source command is to make the current shell read the shell file with the path filepath and execute all the statements in the file in sequence; it is usually used to re-execute the newly modified initialization file so that it takes effect immediately. This command The usage syntax is "source filepath".

What is the use of linux source command?

The operating environment of this article: linux5.9.8 system, PHP7.1 version, DELL G3 computer

Linux source command:

Usual usage: source filepath or . filepath

Function: Make the current shell read the shell file with the path filepath and execute all the statements in the file in sequence. It is usually used to re-execute the just modified initialization file so that it takes effect immediately without having to log out and log in again. For example, when we modify the /etc/profile file and want it to take effect immediately without logging in again, we can use the source command, such as source /etc/profile.

The source command (from the C Shell) is a built-in command of the bash shell; the dot command (.), which is a dot symbol (from the Bourne Shell), is another name for source. This can also be seen from the usage.

The difference between source filepath and sh filepath and ./filepath:

When the shell script has executable permissions, use sh filepath and ./filepath. /filepath makes no difference. ./filepath is because the current directory is not in PATH, and all "." is used to represent the current directory.

sh filepath will re-establish a subshell and execute the statements in the script in the subshell. The subshell inherits the environment variables of the parent shell, but the subshell is newly created and its changed variables will not be carried over. Return to the parent shell, unless export is used.

source filename actually simply reads the statements in the script and executes them in the current shell in sequence, without creating a new subshell. Then all statements that create and change variables in the script will be saved in the current shell.

Recommended study: "linux video tutorial"

Example:

Create a new test .sh script, the content is: A=1;

Modify its executable permission: chmod x test.sh;

After running sh test.sh, echo $A is displayed as empty, Because A=1 is not passed back to the current shell;

After running ./test.sh, the same effect is achieved;

Run source test.sh or . test.sh, and then echo $A, 1 will be displayed, indicating that the variable A=1 is in the current shell;

The above is the detailed content of What is the use of linux source command?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template