Linux environment variable configuration, reprint address: http://blog.sina.com.cn/rss/1650981242.xml

Environment variables are closely related to the shell, and a shell is started after the user logs in to the system. For Linux it is usually bash, but you can reset or switch to another shell (using the chsh command).
Depending on the distribution, bash has two basic system-level configuration files: /etc/bashrc and /etc/profile. These configuration files contain two different sets of variables: shell variables and environment variables. The former is only fixed in a specific shell (such as bash), the latter is fixed in different shells. Obviously, shell variables are local, while environment variables are global. Environment variables are set through Shell commands, and the set environment variables can be used by all programs run by the current user. For the bash shell program, you can access the corresponding environment variables through the variable name, and set the environment variables through export. Note: Linux environment variable names generally use
uppercase letters   $ export MYNAME=”my name is kevin” $ echo $ MYNAME my name is Kevin 3. Modify existing environment variables and continue with the previous example $ MYNAME=”change name to jack” $ echo $MYNAME change name to jack 4. Use env command shows all environment variables $env
















HOSTNAME=localhost.localdomain
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000 SSH_CLIENT
=192.168.136.151 1740 22
QTDIR=/usr/lib/qt-3.1
SSH_TTY=/dev/pts/0  5. Use the set command to display All locally defined shell variables $ set BASH=/bin/bash BASH_ENV=/root/.bashrc …… 6. Use the unset command to clear the environment variables $ export TEMP_KEVIN=”kevin” #Add an environment variable TEMP_KEVIN $ env | grep TEMP_KEVIN #Check whether the environment variable TEMP_KEVIN takes effect (it will take effect if it exists) TEMP_KEVIN=kevin #Prove that the environment variable TEMP_KEVIN already exists $ unset TEMP_KEVIN #Delete the environment variable TEMP_KEVIN $ env | grep TEMP_KEVIN #Check whether the environment variable TEMP_KEVIN has been deleted, no output is displayed, proving TEMP_KEVIN was cleared. 7. Use the readonly command to set read-only variables Note: If the readonly command is used, the variables cannot be modified or cleared.














$ export TEMP_KEVIN = "kevin" #Add an environment variable TEMP_KEVIN
$ readonly TEMP_KEVIN #Set the environment variable TEMP_KEVIN to read-only
$ env | grep TEMP_KEVIN #Check whether the environment variable TEMP_KEVIN is valid
TEMP_KEVIN=kevin #Prove that the environment variable TEMP_KEVIN already exists
$ unset TEMP_KEVIN #It will prompt that this variable is read-only and cannot be deleted-
bash: unset: TEMP_KEVIN: cannot unset: readonly variable
$ TEMP_KEVIN = "tom" #Modifying the variable value to tom will prompt that this variable is read-only and cannot be modified-
bash: TEMP_KEVIN: readonly variable
8. Modify the environment variable by modifying the environment variable definition file.
It should be noted that, under normal circumstances, only modify the environment variable configuration file of ordinary users, and avoid modifying the environment definition file of the root user, because that may cause potential danger.
$ cd ~ #Go to the user root directory
$ ls -a #View all files, including hidden files
$ vi .bash_profile #Modify the user environment variable file
For example:
edit your PATH statement, the format is:
PATH=$PATH:<PATH 1>:<PATH 2>:<PATH 3>:------:<PATH N>
You can add the specified paths by yourself, separated by colons.
After the environment variable is changed, it will take effect the next time the user logs in.
If you want to take effect immediately, you can execute the following statement: $source .bash_profile
It should be noted that it is best not to put the current path "./" in the PATH, which may be subject to unexpected attacks.
Once done, the current search path can be viewed via $ echo $PATH. After this customization, you can avoid frequently starting programs located outside the path searched by the shell.
5. Learning summary
1. The types
of variables in Linux are divided according to the life cycle of the variables. Linux variables can be divided into two categories:
1. Permanent: The configuration file needs to be modified, and the variable takes effect permanently.
2. Temporary: use the export command line to declare, the variable will be invalid when the shell is closed.
2. Three ways to set variables
1. Add variables in /etc/profile file [effective for all users (permanent)]
Use VI to add variables in the file /etc/profile file, the variable will be used for all users under Linux. The user is valid, and is "permanent".
For example: edit the /etc/profile file and add the CLASSPATH variable
# vi /etc/profile
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
Note: To take effect immediately after modifying the file, run # source /etc/ Otherwise, the profile will only take effect the next time the user is re-entered.
2. Add variables to the .bash_profile file in the user directory [effective for a single user (permanent)]
Use VI to add variables to the .bash_profile file in the user directory. The changes are only valid for the current user and are "permanent". of".
For example: edit .bash_profile under the guok user directory (/home/guok)
$ vi /home/guok/.bash.profile
add the following:
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
Note: Modify the file If you want to take effect immediately, you must run $ source /home/guok/.bash_profile, otherwise it will only take effect when the user is re-entered next time.
3. Directly run the export command to define the variable [only valid for the current shell (BASH) (temporary)]
directly use [export variable name=variable value] to define the variable under the shell command line, the variable is only in the current shell (BASH) ) or its sub-shell (BASH) is valid, the shell is closed, and the variable is invalid. When you open a new shell, there is no such variable. If you need to use it, you need to redefine it.

Guess you like

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