Configuration files and environment variables in Linux systems

Configuration file

In Linux systems, commonly used configuration files include:

  • User level configuration file:~/.bashrc
  • System-level configuration files:/etc/profile

There are some differences in functions and effects between user-level configuration files and system-level configuration files:

  1. User level configuration file:

    Location: Typically stored in the user's home directory, e.g.~/.bashrc

    Scope of application: Only effective for the current user, will not affect other users

    Usage scenarios: Suitable for individual user-defined configurations. You can set personal environment variables, aliases, functions, etc.

  2. System-level configuration files:

    Location: By storing in the system's global configuration directory, for example/etc/profile

    Scope of application: It is effective for all users in the system and is a globally shared configuration.

    Usage scenarios: Suitable for administrator or system level configuration, you can set system environment variables, global aliases, system startup scripts, etc.

User-level configuration files have higher priority than system-level configuration files. When the same environment variable exists at both the user level and system level, the user-level configuration will override the system-level configuration.

You can open and edit the configuration file through vim ~/.bashrcthe or gedit ~/.bashrccommand. Vim and gedit are both commonly used text editors.

environment variable

Environment variables can export VARIABLE_NAME=variable_valuebe added to the configuration file by , where VARIABLE_NAMEis the environment variable name and variable_valueis the value of the environment variable.

export PATH=/usr/local/bin:$PATHThis is an example of setting environment variables. The specific explanation is as follows:

  • export: This keyword is used to export the variable to the current shell environment, making it an environment variable
  • PATH: PATHis a special environment variable that contains a colon-separated list of directories. The operating system will use this environment variable to find the path to the executable file. When a command is entered in the terminal, the operating system will PATHsequentially search for the executable file of the command in the defined directory.
  • /usr/local/bin: This is a directory path that will be /usr/local/binadded to PATHthe environment variables. /usr/local/binIs a commonly used directory used to store user-defined executable files
  • $PATH: $PATHIndicates the value of an existing PATHenvironment variable. In this example, add /usr/local/binto PATHthe beginning of the directory listing for the variable, and then add the original value. This is done so that the newly added path can be searched first to avoid conflict with other executable files that may exist with the same name.

In short, export PATH=/usr/local/bin:$PATHit means that will /usr/local/binbe added to PATHthe environment variable and retain the original PATHvalue. In this way, when a command is entered, the system searches /usr/local/binthe directory first and then searches in the original search order. /usr/local/binDoing this will allow you to run the executable file located in the directory directly in the terminal .

You can use echo $VARIABLE_NAMEthe command to verify whether the environment variables are set successfully. For example, you can run echo $PATHto print out PATHthe values ​​of environment variables.

Guess you like

Origin blog.csdn.net/weixin_48158964/article/details/132803825