Linux-environment variables

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) Classification according to effective scope

System environment variables: public, effective for all users.

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

(2) Classification according to life cycle.

Permanent environment variables: 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 will become 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 consist of an underscore, letters (case-sensitive) and numbers. It is customary to use uppercase letters, such as PATH, HOSTNAME, LANG, etc.

2. Commonly used environment variables

1. View environment variables

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

env]grep环境变量名

For example:

env[grep PATH

When using the env command, many environment variables are displayed on the screen, which is inconvenient to view. You can use grep to filter them.

(2) echo command

echo $【环境变量名】

Note that the symbol $ cannot be missing. This symbol is the value of the calling environment variable.

2. Commonly used environment variables

(1)PATH

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 later chapters of this article.

(2) ONLY

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

(3)HOSTNAME

The hostname of the server.

(4)SHELL

The shell parser currently used by the user.

(5)HISTSIZE

Number of saved historical commands

(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 where C/C-++ language dynamic link library files are searched. It is not a default environment variable in Linux, but it is very important for C/C++ programmers. The specific usage is described in detail in the later chapters of this article. CLASSPATH

(10)

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

3. Set the environment quantity

变量名='值'
export 变量名

or:

export 变量名='值'

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

1. System environment variables

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

(1) Set in the /etc/profile file

用户登录时执行/etc/profile文件中设置系统的环境变量。
但是,Linux不建议在/etc/profile文件中设置系统环境变量。

(2) Add the environment variable script file in the /etc/profile.d directory. This is the recommended method for Linux.

/etc/profile在每次启动时会执行/etc/profile.d下全部的脚本文件。
/etc/profile.d目录下有很多脚本文件
/etc/profile.d比/etc/profile好维护,不想要什么变量直接删除

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

该文件配置的环境变量将会影响全部用户使用的bash shell。
但是,Linux也不建议在/etc/bashrc文件中设置系统环境变量。
2. User environment variables

User environment variables only take effect for the current user. There are many ways to set user environment variables.
In the user's home directory, there are several special files that are invisible with ls but can be seen with ls -al.

Insert image description here

(1) .profile (recommended)

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

(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 you exit the system (exit the bash shell).

(4).bash_history

Save commands used by the current user.

3. Execution order of environment variable script files

The execution sequence of environment variable script files is as follows:

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

If the environment variable with the same name is configured in multiple scripts, the configuration in the last executed script shall prevail.

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, the directory needs to be specified when executing.

(1) The PATH environment variable stores a list of directories, separated by colons:, and the final dot indicates the current directory.

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

(2) By default, PATH includes the directory where Linux system commands are located (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin). If these directories are not included, common Linux commands It cannot be executed either (an absolute path must be entered to execute).

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

export PATH=$PATH:HOME/bin

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

2. LANG environment variable

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

Setup of paired LANGs.

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:

LANG=zh_CN.gbk

3. LD_LIBRARY_PATH environment variable

The directory where C/C++ language dynamic link library files are searched. It is not the default environment variable of Linux, but it is very important for C/C++ programmers.
The LD_LIBRARY_PATH environment variable also stores a directory list. The directories are separated by colon:, and the final dot. indicates the current directory, which is in the same format as PATH.

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

4、CLASSPATH

The directory where JAVA language library files are searched is not a Linux default environment variable, but it is very important to JAVA programmers.

The CLASSPATH environment variable also stores a list of directories. The directories are separated by colon:, and the final dot . indicates the current directory, which has the same format as PATH.

5. Effectiveness of environment variables

(1) Under Shell, the environment variable set with export takes effect immediately on the current Shell and becomes invalid after the Shell exits.
(2) The environment variables set in the script file will not take effect immediately. They will only take effect after exiting the Shell and logging in again, or use the source command to make it take effect immediately

     例如:

    `source /etc/profile`

6. Application experience

System environment variables are configured in the /etc/profile.d directory

User environment variables are configured in the user's .bash_profile

Guess you like

Origin blog.csdn.net/qq_45707966/article/details/133999083