centos7 environment variable settings

Table of contents

1. The concept of environment variables

1. The meaning of environment variables

2. Classification of environment variables

3. Linux environment variables

Two, commonly used environment variables

1. View environment variables

1) env command: View all environment variables of the current user.

 2) echo command: View all environment variables of the current user, the symbol $ cannot be missing.

2. Common environment variables

1)PATH

2)LANG

3)HOSTNAME

4)SHELL

5)HISTSIZE

6)USER

7)HOME

8)PWD

9)LD_LIBRARY_PATH

10)CLASSPATH

3. Set environment variables

1. System environment variables

1) Set in the /etc/profile file.

2) Add an environment variable script file in the /etc/profile.d directory, which is the method recommended by Linux.

3) Set environment variables in the /etc/bashrc file.

2. User environment variables

1).bash_profile (recommended first choice)

2).bashrc

3).bash_logout

4).bash_history

3. Execution sequence of environment variable script files

4. Detailed explanation of important environmental variables

1. PATH environment variable

1) The PATH environment variable stores a list of directories, and the directories are separated by colons:, and the last dot. represents the current directory.

2) PATH contains the directory where the Linux system command is located by default

 3) In the user's .bash_profile file, the PATH will be expanded,

4) If the dot . is not included in the PATH variable, you need to add ./ or use an absolute path to execute the program in the current directory.

2. LANG environment variable

3. LD_LIBRARY_PATH environment variable

4、CLASSPATH

5. Effectiveness of environment variables

1) Under the shell, the environment variable set by export takes effect immediately on the current shell, and becomes invalid after the shell exits.

2) The environment variable set in the script file will not take effect immediately, it will take effect only after exiting the Shell and logging in again, or use the source command to make it take effect immediately,

6. Application experience

7. Copyright statement


1. The concept of environment variables

1. The meaning of environment variables

The execution of programs (operating system commands and applications) requires a running environment, which is composed of multiple environment variables.

2. Classification of environment variables

1) Classified according to the effective scope.

System environment variables : public, valid for all users.

User environment variables : user-private and customized personalized settings, which only take effect for this user.

2) Classified by life cycle.

Permanent environment variable : Configured in the environment variable script file, these scripts will be automatically executed every time the user logs in, which is equivalent to permanent effect.

Temporary environment variable : temporarily defined in the shell when used, and becomes invalid after exiting the shell.

3. Linux environment variables

Linux environment variables are also called Shell environment variables. They start with an underscore and a letter and are composed of underscores, letters (case-sensitive) and numbers. It is customary to use uppercase letters, such as PATH, HOSTNAME, LANG, etc.

Two, commonly used environment variables

1. View environment variables

1) env command: View all environment variables of the current user.

Under the Shell, use the env command to view all the environment variables of the current user.

env

The above figure only captures some of the environment variables, not all of them.

When using the env command, many environment variables are displayed on the screen, which is inconvenient to view, and can be filtered by grep.

env|grep 环境变量名

For example, look at environment variables that contain PATH in the environment variable name.

env|grep PATH

 2) echo command: View all environment variables of the current user, the symbol $ cannot be missing.

echo $环境变量名

Note that the symbol $ cannot be missing, which is a grammatical requirement.

2. Common environment variables

1)PATH

The search directory for executable programs. Executable programs include Linux system commands and user applications. The specific usage of the PATH variable is described in detail in the following chapters of this article.

2)LANG

The language, region, character set of the Linux system, and the specific usage of the LANG variable are introduced in detail in the following chapters of this article.

3)HOSTNAME

The hostname of the server.

4)SHELL

The shell parser currently used by the user.

5)HISTSIZE

The number of historical commands to save.

6)USER

The username of the currently logged in user.

7)HOME

The home directory of the currently logged in user.

8)PWD

current working directory.

9)LD_LIBRARY_PATH

The directory searched by the C/C++ language dynamic link library file is not a default environment variable of Linux, but it is very important for C/C++ programmers. The specific usage is described in detail in the chapters later in this article.

10)CLASSPATH

The directory searched by the JAVA language library file is not a default environment variable of Linux, but it is very important for JAVA programmers. The specific usage is described in detail in the following chapters of this article.

3. Set environment variables

变量名='值'
export 变量名

or

export 变量名='值'

If the value of the environment variable has no special symbols such as spaces, it can be included without single quotes.

Example:

export ORACLE_HOME=/oracle/home
export ORACLE_BASE=/oracle/base
export ORACLE_SID=snorcl11g
export NLS_LANG='Simplified Chinese_China.ZHS16GBK'
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin:.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:.

The environment variables set by export will become invalid after exiting the shell, and need to be reset when logging in next time. If you want the environment variable to take effect permanently, you need to configure it in the login script file.

1. System environment variables

System environment variables take effect for all users. There are three ways to set system environment variables.

1) Set in the /etc/profile file.

        When the user logs in, execute the /etc/profile file to set the environment variables of the system. However, Linux does not recommend setting system environment variables in the /etc/profile file.

2) Add an environment variable script file in the /etc/profile.d directory, which is the method recommended by Linux.

/etc/profile will execute all script files under /etc/profile.d every time it starts . /etc/profile.d is easier to maintain than /etc/profile. If you don’t want any variables, just delete the corresponding shell script under /etc/profile.d.

There are many script files in the /etc/profile.d directory, for example:

insert image description here

 In the above example, oracle.sh in the /etc/profile.d directory is the environment variable configuration file of the Oracle database, and the content is as follows:

 

 

3) Set environment variables in the /etc/bashrc file.

The environment variables configured in this file will affect the bash shell used by all users. However, Linux does not recommend setting system environment variables in the /etc/bashrc file.

2. User environment variables

User environment variables only take effect for the current user, and there are many ways to set user environment variables.

In the user's home directory, there are several special files that are lsinvisible to users but visible to users . ls .bash_* 

insert image description here

 

1).bash_profile (recommended first choice)

Executed when the user logs in, each user can use this file to configure their own environment variables.

2).bashrc

This file will be read when the user logs in and every time a new shell is opened. It is not recommended to configure user-specific environment variables in it, because the file will be read every time a shell is opened, and the efficiency will definitely be affected.

3).bash_logout

This file is executed every time the system exits (exits the bash shell).

4).bash_history

The historical commands used by the current user are saved.

3. Execution sequence of environment variable script files

The execution sequence of the environment variable script file is as follows:

/etc/profile->/etc/profile.d->/etc/bashrc->用户的.bash_profile->用户的.bashrc

For environment variables with the same name, if there are configurations in multiple scripts, the configuration in the last executed script shall prevail.

There is another problem that needs attention. The script of /etc/profile.d is executed in /etc/profile . The code is as follows:

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

Therefore, the execution order of /etc/profile.d and /etc/profile depends on how the code is written.

4. Detailed explanation of important environmental variables

1. PATH environment variable

Search directory for executable programs, which include Linux system commands and user applications. If the directory of the executable program is not in the directory specified by PATH, you need to specify the directory when executing it.

1) The PATH environment variable stores a list of directories, and the directories are separated by colons:, and the last dot. represents the current directory.

export PATH=目录1:目录2:目录3:......目录n:.

2) PATH contains the directory where the Linux system command is located by default

(/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin), if these directories are not included, common Linux commands cannot be executed (you must enter an absolute path to execute).

Example:

 3) In the user's .bash_profile file, the PATH will be expanded,

as follows:

export PATH=$PATH:$HOME/bin

4) If the dot . is not included in the PATH variable, you need to add ./ or use an absolute path to execute the program in the current directory.

Example:

insert image description here

 

2. LANG environment variable

The LANG environment variable stores the language, region, and character set of the Linux system. It does not need to be manually set by the system administrator. /etc/profile will call the /etc/profile.d/lang.sh script to complete the PATH setting .

The CentOS6.x character set configuration file is in the /etc/syscconfig/i18n file.

The CentOS7 .x character set configuration file is in the /etc/locale.conf file, and the content is as follows:

insert image description here

 

3. LD_LIBRARY_PATH environment variable

The directory searched by the C/C++ language dynamic link library file. It is not a default environment variable of Linux, but it is very important for C/C++ programmers.

The LD_LIBRARY_PATH environment variable also stores a list of directories. The directories are separated by colons:, and the last dot. represents the current directory, which is in the same format as PATH.

export LD_LIBRARY_PATH=目录1:目录2:目录3:......目录n:.

4、CLASSPATH

The directory that the JAVA language library file searches is not a default environment variable for Linux, but it is very important for JAVA programmers.

The CLASSPATH environment variable also stores a list of directories. The directories are separated by colons:, and the last dot. represents the current directory, which is in the same format as PATH.

5. Effectiveness of environment variables

1) Under the shell, the environment variable set by export takes effect immediately on the current shell, and becomes invalid after the shell exits.

2) The environment variable set in the script file will not take effect immediately, it will take effect only after exiting the Shell and logging in again, or use the source command to make it take effect immediately,

For example:

source /etc/profile

6. Application experience

Although there are many ways to set environment variables, it is recommended that system environment variables be configured in the /etc/profile.d directory, and user environment variables be configured in the user's .bash_profile. Increase the trouble of operation and maintenance, error-prone

7. Copyright statement

C language technology network original article, reproduced please explain the source of the article, the author and the link of the original text.
Source: C Language Technology Network (www.freecplus.net)
Author: Code Nong Youdao

-------------------------------------------------- -------No text below---------------------------------------- --------------

Note: For study only, record questions and reference, encourage each other!

Reference article:

1. CentOS7 set environment variables_C language practical technology blog-CSDN blog

2. Three ways to set environment variables in centos

3. How to modify the path environment variable in linux, and modify the PATH environment variable in linux_Jame Louis' Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/qq_39715000/article/details/125023190