Linux is a multi-user operating system. Multi-user means that each user has his or her own dedicated operating environment after logging into the system. This environment is defined by a set of variables, which are called environment variables. Users can modify their own environment variables to meet environmental requirements.
This type of variable is effective for all users in the system, and all users can use this type of variable. The scope is the entire system.
This file can only be modified under root.
# vi /etc/profile export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib
After the addition is completed, the new environment variable will not take effect immediately. To take effect immediately, you need to run source /etc/profile. Otherwise, it will only take effect the next time you log in as this user.
Add variables to the .bash_profile file in the user directory. This file is a hidden file and can be viewed using ll -a:
$ whoami rethink $ vi /home/rethink/.bash_profile export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib $ source /home/rethink/.bash_profile
In the picture above, two files are enclosed in red boxes: .bashrc and .bash_profile. In principle, when setting such environment variables, it is possible to add them to either of these two files. The difference is: .bash_profile is used to enter the bash shell in the interactive login mode, and .bashrc is used to enter the bash shell in the interactive non-login mode.
It can be understood that the .bash_profile file will only be read once when the user logs in, while .bashrc will be read every time the terminal is opened for a new session.
Temporarily valid environment variables (valid only for the current shell)
Such environment variables are only valid for the current shell. When we log out or close the terminal and reopen it, this environment variable will disappear. It's temporary.
Setting method: Directly use [export variable name = variable value] to define variables from the command line.
$ export NAME="rethink" $ echo $NAME rethink
Common commands for setting environment variables
Update environment variables. Update environment variables and reassign them directly: NAME=’test’ (Note: No $ is required before the variable name);
PATH # echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
The paths are separated by colons. These paths are lists of directories where executable programs can be found. When we enter a command, the shell will first check whether the command is an internal system command. If not, it will then check whether the command is an application. The shell will try to find these applications from PATH.
If the shell does not find the executable file in these path directories, an error will be reported; if it is found, the system will call the execution application. By setting PATH, we can run programs or instructions more conveniently.
Add a directory path to PATH, you can write like this:
$ pwd /root/docker/httpd $ export PATH=$PATH:$PWD $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/docker/httpd `可以看到在PATH后面已经加上了我们当前所处目录的路径`
The user's main working directory is the default directory when the user logs in to the Linux system.
$ whoami rethink $ echo $HOME /home/rethink
The number of historical commands saved. The instructions we enter will be saved by the system. This environment variable records the number of instructions saved. Usually 1000.
$ echo $HISTSIZE 1000 $ HISTSIZE=1001 $ echo $HISTSIZE 1001
Historical commands are saved in memory. When exiting or logging in to the shell, they will be automatically saved or read. We can view them through the history command. You can use the symbol ! to execute a historical command with a specified sequence number. For example, to execute the second historical command, enter !2.
$ history 5 59 ls 60 who 61 history | head -n 5 62 who am i 63 history 5 $ !62 who am i root pts/0 2018-04-04 11:00 (101.81.92.18)
Current user login name.
$ echo $LOGNAME rethink
$ echo $HOSTNAME JDu4e00u53f7
当前用户使用的shell种类。
$ echo $SHELL /bin/bash
The above is the detailed content of Summary of Linux environment variables. For more information, please follow other related articles on the PHP Chinese website!