Summary of Linux environment variables
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
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:

$ 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
- 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
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后面已经加上了我们当前所处目录的路径`
- 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
- 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
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)
- LOGNAME
Current user login name.
$ echo $LOGNAME rethink
- HOSTNAME 主机名称。
$ echo $HOSTNAME JDu4e00u53f7
- SHELL
当前用户使用的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!

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor