Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the source command in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, Xiaobian will bring you about how to use the source command in Linux. The article is rich in content and analyzes and narrates it from a professional perspective. After reading this article, I hope you can gain something.

Usage: source filename # filename must be an executable script file or. filename #Note ". There is also a space function after the "sign:

Tells the current shell to read the file with filename and execute all the statements in the file in turn.

Usually used to re-execute the initialization file you just modified so that it takes effect immediately, rather than logging out and logging back in. For example, when we modify the/etc/profile file and want it to take effect immediately without having to log in again, we can use the source command, such as "source /etc/profile."

The source command is a built-in command of the bash shell. And the point command (i.e.". "Command" is another name for source. Both commands take a script as an argument that executes as the environment of the current shell, i.e., does not start a new child process. All variables set in the script become part of the current Shell.

Applications:

In view of the above description, the simplest application scenario is that when cross-compiling, the source command is used in the current shell to temporarily specify the compilation environment of the system as an embedded compilation environment, and then compile and debug the source code. In another shell, or when you close the shell, everything goes back to normal.

For example, recently engaged in I.MX6 embedded development, involving the kernel and device tree compilation debugging, fsl official in its development environment installation package, configured such a shell script, located in/opt/poky/1.7 directory, directly in the shell input the following command to import the shell script:

source /opt/poky/1.7/environment-setup-cortexa9hf-vfp-neon-poky-linux-gnueabi

Then you can compile and develop kernel and device tree based on poky environment, which is very convenient.

Compare:

"source filename" and "sh filename","./ filename"All three commands can be used to execute a script file, so what's the difference between them?

When shell scripts have executable privileges, use sh filename with./ filename is indistinguishable./ filename is because the current directory is not in PATH, so ". "is used to represent the current directory.

sh filename creates a new child shell in which the script is executed. The child shell inherits environment variables from the parent shell, but the child shell is new and its changed variables are not brought back to the parent shell unless export is used.

The statements in the source filename read script are executed in turn in the current shell without creating a new subshell. All new and variable changes in the script will be saved in the current shell.

Examples:

Create a new test.sh script with the following content:A=1;

Modify its executable permissions: chmod +x test.sh;

After running sh test.sh, echo $A, appears empty because A=1 is not returned to the current shell;

Run./ After test.sh, it has the same effect;

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

The above is how to use the source command in Linux shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report