Types of variables for Linux
According to the lifetime of the variables, the Linux variables can be divided into two categories:
1 Permanent: The configuration file needs to be modified, and the variable will take effect permanently.
2 Temporary: With the Export command declaration, the variable is invalidated when the shell is closed.
Three ways to set a variable
1 adding variables to the/etc/profile file "in effect for all users (permanent)"
Using VI to add a variable to the file/etc/profile file, the variable will be valid for all users under Linux and is "permanent".
Example: Edit the/etc/profile file, add the CLASSPATH variable
# Vi/etc/profile
Export classpath=./java_home/lib; $JAVA _home/jre/lib
Note: If you want to run the file immediately after you modify it, you can only take effect the next time you re-enter the user. Source/etc/profile.
2 Add variable "to single user (permanent)" in. bash_profile file in User directory
Use VI to add variables to the. bash_profile file in the user directory, and change the amount to be valid only for the current user and be "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: If you want to take effect immediately after modifying a file, run $ source/home/guok/.bash_profile or you can only take effect the next time you re-enter the user.
3 run the Export command directly define the variable " valid only for the current shell (BASH)"
Define the variable directly under the shell's command line using [export variable name = variable value].
This variable is valid only under the current shell (bash) or its child shell (bash),
When the shell is closed, the variable is invalidated, and when the new shell is opened there is no such variable, and it needs to be redefined.
View of 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.
To delete a specified environment variable using unset
Set sets the value of an environment variable. Clear the value of the environment variable with the unset command. If no value is specified, the value of the variable is set to NULL. Examples are as follows:
$ export test= "TEST ..." #增加一个环境变量TEST
$ env|grep Test #此命令有输入, proving that the environment variable test already exists
Test=test ...
UNset TEST #删除环境变量TEST
$ env|grep Test #此命令没有输出, proving that the environment variable test has been deleted
Common environment variables
PATH determines to which directories the shell will look for commands or programs
Home Current User Home directory
Histsize Number of historical records
LOGNAME The current user's login name
HOSTNAME refers to the name of the host
Shell Current User shell type
Languge language-related environment variables, multiple languages can modify this environment variable
Mail storage directory for the current user of mail
PS1 basic prompt, for root user Yes #, for normal user is $
setting and viewing environment variables under Linux