Home > System Tutorial > LINUX > body text

Summary of Linux environment variables

PHPz
Release: 2024-02-10 09:39:26
forward
924 people have browsed it

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.

How to set environment variables

  • Permanent variables that are valid for all users

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
Copy after login

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.

  • Permanent variables that are effective for a single 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:

Linux 环境变量总结
$ whoami 
rethink

$ vi /home/rethink/.bash_profile
export CLASSPATH=./JAVA_HOME/lib:$JAVA_HOME/jre/lib 

$ source /home/rethink/.bash_profile
Copy after login

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
Copy after login

Common commands for setting environment variables

  • echo is used to print and display environment variables, such as: echo $NAME;
  • export is used to set new environment variables, such as: export NAME=’rethink’;

Update environment variables. Update environment variables and reassign them directly: NAME=’test’ (Note: No $ is required before the variable name);

  • env displays the variables of the current user;
  • set displays the current shell variables, which contain user variables;
  • unset deletes an environment variable, such as: unset NAME;
  • readonly sets the environment variable to be read-only, such as: readonly NAME. The read-only variable unset is invalid.

Common environment variables

PATH
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Copy after login

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后面已经加上了我们当前所处目录的路径`
Copy after login
  • HOME

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
Copy after login
  • HISTSIZE

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
Copy after login

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)
Copy after login
  • LOGNAME

Current user login name.

$ echo $LOGNAME
rethink
Copy after login
  • HOSTNAME 主机名称。
$ echo $HOSTNAME
JDu4e00u53f7
Copy after login
  • SHELL

当前用户使用的shell种类。

$ echo $SHELL
/bin/bash
Copy after login

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!

source:lxlinux.net
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template