Detailed explanation of the role of source

The role of the source /etc/profile file
After saving the configuration using source /etc/profile under linux, the new environment variables can only be effective in one terminal.

Problem description:
After adding environment variables in /etc/profile, it can only take effect in the current terminal after compiling with source /etc/profile;

After restarting a terminal, the environment variable becomes invalid.

Solution:
Restart the system: reboot, the problem is solved;

Because the environment variable set does not really take effect, just use the source command to temporarily run it.

Environment variable setting method:
1. /etc/profile: It is the first file used by the operating system to customize the user environment. This file sets the environment information for each user of the system. When the user logs in for the first time, the file is executed.
2. /etc/environment: The second file used by the operating system when logging in. The system sets environment variables before reading your own profile.
3. ~/.bash_profile: The third file used when logging in is the .profile file. Each user can use this file to input shell information dedicated to his own use. When the user logs in, the file is executed only once. By default, some environment variables are set and the user's .bashrc file is executed.

/etc/bashrc: Execute this file for each user who runs the bash shell. When the bash shell is opened, the file is read.
4. ~/.bashrc: This file contains bash information dedicated to your bash shell. This file is read when you log in and every time you open a new shell.

Set permanent environment variables
1. In the environment variable configuration, first delete the three lines of .bashrc definition in .bash_profile, and then configure the environment variables in .bashrc
2. Select the java environment to be used: update-alternatives -config java
3. To make the newly modified environment variables take effect: source.bashrc
4. View environment variables: env
can be placed in /etc/bash/bashrc, which is system-level

Source command usage:
source FileName: Function: Read and execute the commands in FileName in the current bash environment.

Note: This command is usually replaced by the command ".", such as: source .bash_rc and .bash_rc are equivalent.

Note: The difference between source command and shell scripts is: source executes commands in the current bash environment, while scripts starts a subshell to execute commands. In this way, if the command to set environment variables (or alias, etc.) is written into scripts, it will only affect the subshell and cannot change the current BASH. Therefore, when setting environment variables through a file (command line), use the source command.

Guess you like

Origin blog.csdn.net/qq_41536934/article/details/114096925