Detailed explanation of Linux environment variables

environment variable

First, the definition: the system predefined parameters

       Specify a directory. When running the software, the related programs will search for related files according to the directory. If the environment variable is not set, the prompt "command not found" will appear when the command is typed, because the system does not search in the environment variable. the order

Second, the types of Linux environment variables, divided according to the life cycle

      1. Permanent: The configuration file needs to be modified, and the variable takes effect permanently

      2. Temporary: use the export command to declare, the variable will be invalid when the shell is closed

Three, three methods of setting variables

    1. This time the boot takes effect (temporary), the next boot will be invalid

    enter:

export PATH=$PATH:/usr/local/bin

    2. Only make the current user take effect (permanently)

    enter:

1 、 vi ~ / .bash_profile
2. Add to the last line: PATH=$PATH:$HOME/bin:/usr/local/bin
3. Immediately effective: source ~/.bash_profile

    3. Effective for all users (permanent)

    enter:

1、vi /etc/profile
2. Enter at the end of the file: export PATH=$PATH:/usr/local/bin
3. Immediately effective: source /etc/profile

    Note: files related to environment variables may also include /etc/bashrc, etc. This file is only valid for shells, and /etc/profile is global

 

Fourth, the order of execution of environment variables

    To log in to Linux, first start the /etc/profile file, and then start ~/.bash_profile, ~/.bash_login or ~/.profile in the user directory

    One of the files, the order of execution is: ~/.bash_profile, ~/.bash_login, ~/.profile

    The ~/.bashrc file is executed in ~/.bash_profile:      

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
be

    In ~/.bashrc:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
          . /etc/bashrc
be

    above all,

    The execution sequence is: /etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout

 Regarding the scope of each file, I found the following instructions online:

  (1) /etc/profile: This file sets the environment information for each user of the system. When the user logs in for the first time, this file is executed.

           And collect the shell settings from the configuration files in the /etc/profile.d directory.

  (2) /etc/bashrc: Execute this file for each user who runs the bash shell. When the bash shell is opened, the file is read.

  (3) ~/.bash_profile: Each user can use this file to enter shell information dedicated to their own use. When the user logs in,

           This file is executed only once! By default, it sets some environment variables and executes the user's .bashrc file.

  (4) ~/.bashrc: This file contains bash information specific to your bash shell, when you log in and every time you open a new shell,

           The file is read.

  (5) ~/.bash_logout: This file is executed every time you exit the system (exit the bash shell). In addition, the variables (global) set in /etc/profile are

          It can act on any user, and the variables (local) set in ~/.bashrc can only inherit the variables in /etc/profile, and they have a "parent-child" relationship.

  (6) ~/.bash_profile is an interactive, login mode to enter bash to run ~/.bashrc is an interactive non-login mode to enter bash

         Usually the two are run with roughly the same settings, so usually the former will call the latter.

 

5. Soft links

    When executing a command, if you encounter "command not found", you can also use adding a soft link to solve it

    Soft link: Also known as a symbolic link, this file contains the pathname of another file. Can be any file or directory,

                  You can link files of different file systems, similar to the shortcuts below Windows

    How to use: ln -s source file object file

    Example:

ln -s /tmp/mysql5/bin/mysql /usr/local/bin/mysql

     If /usr/local/bin is under the environment variable, you can directly type mysql and log in to the mysql client at this time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326474105&siteId=291194637