Detailed explanation of source command under Linux

Analysis of execution mode of Linux source command and script

When I modify the / etc / profile file, I want it to take effect immediately without logging in again; At this time, I thought of using the source command, such as source / etc / profile, to learn source, and compared it with the SH execution script. Now I'll summarize.

Source command:

The source command is also called "point command", that is, a point symbol (.), which is the internal command of bash.

Function: make the shell read in the specified shell program file and execute all statements in the file in turn. The source command is usually used to re execute the newly modified initialization file to make it take effect immediately without logging off and logging in again.

Usage:

Source filename or Filename Source command (from C shell) is a built-in command of bash shell; Point command (.), Just a dot symbol (from Bourne shell) is another name of source.

Source filename and sh filename/ What's the difference between filename executing scripts?

1. When the shell script has executable permission, use sh filename and/ Filename makes no difference in executing scripts/ Filename is because the current directory is not in the path. All directories are "." Is used to represent the current directory. 2. Sh filename recreates a child shell and executes the statements in the script in the child shell. The child shell inherits the environment variables of the parent shell, but the new and changed variables of the child shell will not be brought back to the parent shell unless export is used. 3. Source filename: this command simply reads the statements in the script and executes them successively in the current shell without creating a new sub shell. Then all new and changed variable statements in the script will be saved in the current shell.

For example:

1. Create a new test SH script, content: a = 1

2. Then make it executable Chmod + X test sh

3. Run sh test After sh, echo $a is empty because a = 1 is not returned to the current shell

4. Operation/ test. After sh, it has the same effect

5. Run source test SH or test. SH, then echo $a, and 1 will be displayed, indicating that the variable a = 1 is in the current shell

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>