How to Set and view Linux environment variables: Source: network collection favorites copy shared comments (0)
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
History Size = 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. Run the unset command to clear environment variables.
Set can be used to set the value of an environment variable. Run the unset command to clear the environment variable values. If no value is specified, the variable value is set to null. Example:
$ Export test = "test..." # Add an environment variable test.
$ ENV | grep test # This command is input to prove that the environment variable test already exists.
Test = test...
$ Unset $ test # Delete the environment variable test
$ ENV | grep test # This command is not output, proving that the environment variable test already exists
6. Use the readonly command to set the read-only variable
If the readonly command is used, the variables cannot be modified or cleared. Example:
$ Export test = "test..." # Add an environment variable test.
$ Readonly test # Set the environment variable test as read-only
$ Unset test # This variable cannot be deleted
-Bash: unset: Test: cannot unset: readonly variable
$ Test = "new" # This variable cannot be modified.
-Bash: Test: readonly variable
The environment variable settings are in the/etc/profile file.
If you want to add new environment variables, you can add subordinate lines.
Export Path = $ path:/path1:/path2:/pahtn
Bytes -----------------------------------------------------------------------------------------------------------------------
1. Linux variable types
Linux variables are divided by the life cycle of variables. There are two types of Linux variables:
1.1 permanent: the configuration file needs to be modified, and the variable takes effect permanently.
1.2 temporary: Use the Export command to declare the variable. The variable becomes invalid when the shell is closed.
2. Three Methods for setting Variables
2.1 Add a variable to the/etc/profile file [effective for all users (permanent )]
Use VI to add a variable to the/etc/profile file, which will be valid for all users in Linux and "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 change the file to take effect immediately, run # source/etc/profile. Otherwise, it will only take effect the next time you re-enter the user.
2.2 Add a variable to the. bash_profile file in the user directory [effective for a single user (permanent )]
Use VI to add a variable to the. bash_profile file in the user directory. The change volume is only valid for the current user and is "permanent ".
For example, edit the. 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: to change the file to take effect immediately, run $ source/home/guok/. bash_profile. Otherwise, it will only take effect the next time you re-enter the user.
2.3 run the export command directly to define the variable [only valid for the current shell (BASH) (Temporary )]
Use the [export variable name = variable value] to define a variable under the shell command line. This variable is valid only in the current shell (BASH) or its subshell (BASH, when shell is closed, the variable becomes invalid. This variable is not available when a new shell is opened. You need to define the variable again if necessary.
3. view environment variables
3.1 run 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. Common Environment Variables
PATH determines the directories to which shell will look for commands or programs.
HOME main directory of the current user
HISTSIZE
LOGNAME: The Login Name of the current user.
HOSTNAME indicates the host name.
SHELL Current User Shell Type
LANGUGE language-related environment variables, which can be modified in multiple languages
MAIL current user's MAIL storage directory
PS1 basic prompt, for root users is #, for common users is $