Three methods Linux environment variable settings

First, the type of the variable linux

According to life cycle variables, linux variables can be divided into two categories:

1. permanent: the need to modify the configuration file, variable permanent.

2. Temporary: Use the export command to declare the variable fail when you close the shell.

Two, three methods set variables

1, take effect for all users (permanent)

Variables increase in / etc / profile files, this variable will take effect for all users of Linux, and is permanent. 
For example: editing / etc / profile file, add the JAVA_HOME variable:

[root@centos76 ~]# vim /etc/profile
JAVA_HOME=/usr/local/java/jdk1.8.0_221
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME

Note: After modifying the file you wish to immediately take effect with the following commands, or only take effect when users log back here next time.

[root@centos76 ~]# source /etc/profile

2, entered into force for a particular user (permanent)

Increase in the .bash_profile file in the home directory of a particular user variable, the amount of change will only current user, and is permanent. 
For example: .bash_profile under the Edit / home / boxiaoyuan directory

[root@centos76 ~]# vim /home/boxiaoyuan/.bash_profile
JAVA_HOME=/usr/local/java/jdk1.8.0_221
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME

Note: After modifying the file in order to take effect immediately but also run the following command, or can only take effect the next time the user re-enter.

[root@centos76 ~]# source /home/boxiaoyuan/.bash_profile

3, only valid for the current shell (temporary)

When used in a command shell directly [export variable name = variable value] defined variable that is only in the current sub-shell or a shell to be effective, closed shell, variables also fails, then open a new shell this variable will not need to use words still need to be redefined.

Third, the viewing environment variable

1, using the echo command to view the individual environment variables.

[boxiaoyuan@centos76 ~]$ echo $PATH
/usr/local/java/jdk1.8.0_221/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/boxiaoyuan/.dotnet/tools:/build4/tools/projectdeploy:/home/boxiaoyuan/.local/bin:/home/boxiaoyuan/bin

2, using the env view all environment variables.

[boxiaoyuan@centos76 ~]$ env
XDG_SESSION_ID=17743
HOSTNAME=centos76
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
QT_GRAPHICSSYSTEM_CHECKED=1
USER=boxiaoyuan

 

Guess you like

Origin www.cnblogs.com/zhuzhaoli/p/11658245.html