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

How to set and view Linux environment variables

高洛峰
Release: 2016-12-15 16:54:20
Original
1389 people have browsed it

 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 to display 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. An 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 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 new environment variables, you can add subordinate lines

 Export path=$path:/path1:/path2:/pahtN

 -------------------------------- -------------------------------------------------- --------------------------------------------------

 1. Variable types in Linux

Divided according to the life cycle of variables, 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, which will It will be effective 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: The modification of the file must take effect immediately Also run # source /etc/profile, otherwise it will only take effect the next time you log in as this user.

 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 changes will only be effective for the current user, and It is "permanent".

  For example: Edit .bash_profile

  $ 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)]

  Directly use [export variable name = variable value] to define variables under the shell command line. This variable is only available in the current shell. It is valid under shell (BASH) or its subshell (BASH). When the shell is closed, the variable will become invalid. When you open a new shell, there will be no such variable. 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 the specified environment variable.

  4. Commonly used environment variables

PATH determines which directories the shell will search for commands or programs

HOME The current user’s home directory

HISTSIZE The number of historical records

LOGNAME The login name of the current user

HOSTNAME refers to the host Name

 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 it is $



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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template