Home >php教程 >PHP开发 >How to set and view environment variables in Linux

How to set and view environment variables in Linux

高洛峰
高洛峰Original
2016-12-17 13:30:061352browse

1. Display the environment variable HOME
$ echo $HOME
/home/redbooks

2. Set a new environment variable hello
$ export HELLO="Hello!"
$ echo $HELLO
Hello!

3. Use The env command displays all environment variables
$ env
HOSTNAME=redbooks.safe.org
PVM_RSH=/usr/bin/rsh
Shell=/bin/bash
TERM=xterm
HISTSIZE=1000
...

4. Use the set command to display all locally defined Shell variables
$ set
BASH=/bin/bash
BASH_VERSINFO=([0]="2"[1]="05b"[2]="0"[3]=" 1"[4]="release"[5]="i386-redhat-linux-gnu")
BASH_VERSION='2.05b.0(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=80
DIRSTACK=()
DISPLAY=:0.0
...

5. Use the unset command to clear environment variables
set can set the value of an environment variable. To clear the value of an environment variable, use the unset command. If no value is specified, the variable value will be set to NULL. The example is as follows:
$ export TEST="Test..." #Add an environment variable TEST
$ env|grep TEST #This command has input, proving that the environment variable TEST already exists
TEST=Test...
$ unset $ TEST #Delete environment variable TEST
$ env|grep TEST #This command has no output, proving that the environment variable TEST already exists

6. Use the readonly command to set read-only variables
If the readonly command is used, the variable cannot be modified or cleared. Examples are as follows:
$ export TEST="Test..." #Add an environment variable TEST
$ readonly TEST #Set the environment variable TEST to read-only
$ unset TEST #You will find that this variable cannot be deleted
-bash: unset : TEST: cannot unset: readonly variable
$ TEST="New" #You will find that this variable cannot be modified
-bash: TEST: readonly variable
The environment variable settings are located in the /etc/profile file
If you need to add a new environment Variables can add subordinate lines
export path=$path:/path1:/path2:/pahtN
-------------------------------- -------------------------------------------------- ------------------------------------------
1. Variable types in Linux
By variable Divided by life cycle, Linux variables can be divided into two categories:
1.1 Permanent: The configuration file needs to be modified, and the variable takes effect permanently.
1.2 Temporary: Just declare it using the export command. The variable will become invalid when the shell is closed.

2. Three ways to set variables
2.1 Add variables in the /etc/profile file [Effective for all users (permanent)]
Use VI to add variables in the file /etc/profile file, the variable will be It is valid for all users under Linux 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: After modifying the file, you need to run it if you want it to take effect immediately# source /etc/profile Otherwise it will only take effect the next time the user logs in again.
2.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 change will only be effective for the current user and is " permanent".
For example: edit .bash_profile under the guok user directory (/home/guok)
$ vi /home/guok/.bash.profile
Add the following content:
export CLASSPATH=./JAVA_HOME/lib;$JAVA_HOME/jre/lib
Note: If you want the modified file to take effect immediately, you must run $ source /home/guok/.bash_profile, otherwise it will only take effect the next time you log in as this user.
2.3 Directly run the export command to define variables [valid only for the current shell (BASH) (temporary)]
Use [export variable name = variable value] directly from the shell command line to define variables. This variable is only available in the current shell ( BASH) or its subshell (BASH). When the shell is closed, the variable becomes invalid. When you open a new shell, the variable will not exist. If you need to use it, you need to redefine it.

3. Viewing environment variables
3.1 Use the echo command to view a single environment variable. For example:
echo $PATH
3.2 Use env to view all environment variables. For example:
env
3.3 Use set to view all locally defined environment variables.
unset can delete specified environment variables.

4. Commonly used environment variables
PATH determines which directories the shell will search for commands or programs
HOME current user’s home directory
HISTSIZE number of historical records
LOGNAME current user’s login name
HOSTNAME refers to the name of the host
SHELL current user Shell type
LANGUGE Language-related environment variables, multi-language can modify this environment variable
MAIL Current user’s mail storage directory
PS1 Basic prompt, for root users is #, for ordinary users is $


For more articles on how to set up and view environment variables in Linux, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn