Linux environment variables and linux environment variable settings
Address:Http://www.cnblogs.com/archimedes/p/linux-envionment-variables.html, Reprinted, please specify the source address.
1. What is an environment variable?
Bash shell uses a feature called environment variable to store information about shell sessions and work environments. It allows you to store data in memory for access by programs and scripts running on shell.
In bash shell, environment variables are classified into two types: global variables and local variables.
(1) Global Environment Variables
Global environment variables are not only visible to shell sessions, but also useful for programs that need to obtain parent process information in all sub-processes created by shell. In Linux, some global environment variables are set before you start a bash session.
All system environment variables are written in uppercase to distinguish them from the environment variables of common users.
Run the printenv command to view global variables)
To display a single environment variable, you can use the echo command. When you reference an environment variable, You must place a $ character before the name of the environment variable:
Wu @ ubuntu :~ /Ccode $ echo $ HOME
/Home/wu
(2) Local Environment Variables
Local environment variables can only be seen in the process of defining them. It is a bit complicated to view the list of local environment variables. in Linux, no such command only displays local environment variables.
The set command displays all environment variables set by a specific process, including global variables.
2. Set environment variables (1) set local environment variables
Once bash shell is started, you can create local variables visible in the shell process. You can assign values to environment variables by equal signs. The values can be numerical values or strings:
Wu @ ubuntu :~ $ Test = testing
Wu @ ubuntu :~ $ Echo $ test
Testing
To assign a variable a string containing spaces, you must use single quotes to define the start and end of the string:
Wu @ ubuntu :~ $ Test = testing a long string
A: command not found
Wu @ ubuntu :~ $ Test = 'testing a long string'
Wu @ ubuntu :~ $ Echo $ test
Testing a long string
(2) set global environment variables
To create a global environment variable, first create a local environment variable and then export it to the global environment.
This process is completed using the export command:
Wu @ ubuntu :~ $ Echo $ test
Testing a long string
Wu @ ubuntu :~ $ Export test
Wu @ ubuntu :~ $ Bash
Wu @ ubuntu :~ $ Echo $ test
Testing a long string
(3) Delete Environment Variables
You can use the unset command to delete environment variables:
Wu @ ubuntu :~ $ Echo $ test
Testing a long string
Wu @ ubuntu :~ $ Unset test
Wu @ ubuntu :~ $ Echo $ test
Wu @ ubuntu :~ $
3. default shell Environment Variables
1. BASH: record the path of the current bash shell.
2. BASH_SUBSHELL: record the hierarchy of the current sub-shell. BASH_SUBSHELL is an integer counted from 0.
3. BASH_VERSINFO: An array contains six elements, which display the version information of bash.
4. BASH_VERSION: displays the shell version.
5. DIRSTACK: records the directory value at the top of the stack. The initial value is empty.
6. GLOBLGNORE: A mode list separated by colons, indicating a collection of file names ignored during configuration.
7. GROUPS: record the group to which the current user belongs.
8. HOME: record the HOME Directory of the current user, which is determined by the penultimate domain of/etc/passwd.
9. HOSTNAME: record the host name.
10. HOSTTYPE and MACHTYPE: record the hardware architecture of the system.
11. IFS: used to set the specified shell domain separator, which is a space by default.
12. OLDPWD: record the old working directory.
13. OSTYPE: record the operating system type.
14. PATH: environment variable. The content of the current PATH environment variable is displayed.
15. PPID: indicates the process ID of the current process, that is, the parent process ID of the current process.
16. PS1: the prompt variable, used to set the prompt format, used to set the first-level shell prompt environment variable.
17. PS2: used to set the second-level shell prompt environment variables.
18. PWD: record the current path
19. REPLY: The REPLY variable is related to read and select.
20. SECONDS: record the time consumed by the script from the beginning to the end.
21. SHELL: display the current shell
22. SHELLOPTS: records the list of shell options in the "on" state. It is only a read-only variable.
23. SHLVL: record the layer of bash nesting. Generally, when we start the first Shell. $ SHLVL = 1. If you execute the script in this Shell, $ SHLVL = 2 in the script.
24. TMOUT: used to set the script expiration time. For example, TMOUT = 3 indicates that the script expires 3 seconds later.
25. UID: ID of the logged-in user
26. USER: displays the current USER name
4. Set the PATH environment variable
PATH defines the search PATH for command line Input. If the command cannot be found, it will generate an error:
Wu @ ubuntu :~ $ Myprog
Myprog: command not found
You can add a new search directory to an existing PATH environment variable. You do not need to define it from the beginning. The directories in the PATH are separated by colons. Therefore, you only need to reference the original PATH value, then add a new directory to the string.
5. variable array
A good function of environment variables is that they can be used as arrays. To set multiple values for an environment variable, you can put the values in parentheses, and separate the values with spaces:
Wu @ ubuntu :~ $ Mytest = (one two three four five)
Wu @ ubuntu :~ $ Echo $ mytest
One
Only the first value is displayed. To reference a separate array element, you must use the value index that represents its position in the array. The value must be enclosed in square brackets:
Wu @ ubuntu :~ $ Echo $ {mytest [2]}
Three
To display the entire array variable, the asterisk can be used as a wildcard to place the index value:
Wu @ ubuntu :~ $ Echo $ {mytest [*]}
One two three four five
You can also change the value of an index:
Wu @ ubuntu :~ $ Mytest [2] = seven
Wu @ ubuntu :~ $ Echo $ {mytest [*]}
One two seven four five
You can even use the unset command to delete a value in the array, but be careful:
Wu @ ubuntu :~ $ Unset mytest [2]
Wu @ ubuntu :~ $ Echo $ {mytest [*]}
One two four five
Wu @ ubuntu :~ $ Echo $ {mytest [2]}
Wu @ ubuntu :~ $ Echo $ {mytest [3]}
Four
Finally, you can follow the array name after the unset command to delete the entire array:
Wu @ ubuntu :~ $ Unset mytest
Wu @ ubuntu :~ $ Echo $ {mytest [*]}
Wu @ ubuntu :~ $
Changing Environment Variables in linux
Linux variables are divided by the life cycle of variables. They can be divided into two types:
(1) permanent: the configuration file needs to be modified, and the variable takes effect permanently.
Common configuration files include:
(1-1)/etc/profile: This file takes effect for all users. This file sets the environment information for each user in the system. This file is executed when the user logs on for the first time; and from/etc/profile. configure shell collection in the configuration file of the d directory
For example, edit the/etc/profile file and add the CLASSPATH variable.
# Vi/etc/profile
Add a row:
Export CLASSPATH =./JAVA_HOME/lib; $ JAVA_HOME/jre/lib
After modification, You need to execute a new login to take effect, you can also execute the command source/etc/profile to take effect
(1-2)/etc/bashrc: effective for all users. Execute this file for every user running bash shell. When bash shell is opened, this file is read
The editing method is described above.
(1-3 )~ /. Bash_profile: it is only valid for the current user. Each user can use this file to enter the shell information dedicated to his/her use. When the user logs on, the file is executed only once.
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
After modification, You need to execute a new login to take effect, you can also execute the command source/etc/profile to take effect
(1-4 )~ /. Bashrc: only valid for the current user. The file contains bash information dedicated to your bash shell. The file is read when you log on and each time you open a new shell.
The editing method is described above.
In addition ,~ The variables (local) set in/. bashrc can only inherit the variables in/etc/profile. They are "Parent-Child" relationships.
Summary: to modify the above files, add the variables you need. When you start a shell (terminal, terminal), all the variables you define will take effect.
(2) Temporary: Use the export command to declare the variable. The variable is valid only in the current shell (BASH) or its subshell (BASH) and fails after the shell is closed, this variable is not available when the new shell is opened. You need to redefine it if you need to use it.
Use the [export variable name = variable value] to define the variable under the shell command line.
View environment variables
(1) Use the echo command to view a single environment variable. For example:
Echo $ PATH
(2) Use env to view all environment variables. For example:
Env
(3) Use set to view all locally defined environment variables. For example:
Set
In addition, unset can delete specified environment variables.
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
LANG ...... remaining full text>
Linux environment variable settings
1. Use the echo command to display environment variables
In this example, echo is used to display common variables HOME.
$ Echo $ HOME
/Home/kevin
2. Set a new environment variable
$ Export MYNAME = "my name is kevin"
$ Echo $ MYNAME
My name is Kevin
3. Modify existing environment variables
Example
$ MYNAME = "change name to jack"
$ Echo $ MYNAME
Change name to jack
4. Use the env command to display all environment variables
$ Env
HOSTNAME = localhost. localdomain
SHELL =/bin/bash
TERM = xterm
History Size = 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. Run the unset command to clear 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 (if any)
TEMP_KEVIN = kevin # verify 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. If no output is displayed, it indicates that TEMP_KEVIN has been cleared.
7. Use the readonly command to set the read-only variable
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 takes effect
TEMP_KEVIN = kevin # verify that the environment variable TEMP_KEVIN already exists
$ Unset TEMP_KEVIN # The system will prompt that this variable cannot be deleted only.
-Bash: unset: TEMP_KEVIN: cannot unset: readonly variable
$ TEMP_KEVIN = "tom" # Changing the variable value to tom will prompt that the variable read-only cannot be modified.
-Bash: TEMP_KEVIN: readonly variable
8. Modify the environment variable definition file.
In general, only the environment variable configuration file of the common user is modified to avoid modifying the environment definition file of the root user, which may cause potential risks.
$ Cd ~ ... The remaining full text>