Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure environment variables in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to configure environment variables in Linux, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Linux reads environment variables

The method of reading environment variables:

The export command displays all environment variables defined by the current system

The echo $PATH command outputs the value of the current PATH environment variable

The effect of these two commands is as follows

Uusama@ubuntu:~$ exportdeclare-x HOME= "/ home/uusama" declare-x LANG= "en_US.UTF-8" declare-x LANGUAGE= "en_US:" declare-x LESSCLOSE= "/ usr/bin/lesspipe% s" declare-x LESSOPEN= "| / usr/bin/lesspipe% s" declare-x LOGNAME= "uusama" declare-x MAIL= "/ var/mail/uusama" declare-x PATH= "/ home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/ Local/bin:/usr/sbin:/usr/bin:/sbin:/bin "declare-x SSH_TTY=" / dev/pts/0 "declare-x TERM=" xterm "declare-x USER=" uusama "uusama@ubuntu:~$ echo $PATH/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The PATH variable defines the lookup path to run the command with a colon: split different paths, with or without double quotation marks when using the export definition.

Linux environment variable configuration method 1: export PATH

Use the export command to directly modify the value of PATH and configure the method for MySQL to enter the environment variable:

Export PATH=/home/uusama/mysql/bin:$PATH# or put PATH in front of export PATH=$PATH:/home/uusama/mysql/bin

Note:

Effective time: effective immediately

Validity period: the current terminal is valid and invalid after the window is closed

Effective scope: valid for current users only

Don't forget to add the original configuration, that is, $PATH, to the environment variable of the configuration to avoid overwriting the original configuration.

Linux environment variable configuration method 2: vim ~ / .bashrc

Configure by modifying the ~ / .bashrc file in the user directory:

Vim ~ / .bashrc # add export PATH=$PATH:/home/uusama/mysql/bin to the last line

Note:

Effective time: when a new terminal is opened with the same user, or manual source ~ / .bashrc takes effect

Effective period: permanently valid

Effective scope: valid for current users only

If a subsequent environment variable load file overrides the PATH definition, it may not take effect

Linux environment variable configuration method 3: vim ~ / .bash_profile

Similar to modifying the ~ / .bashrc file, you need to add a new path to the end of the file:

Vim ~ / .bash_profile# adds export PATH=$PATH:/home/uusama/mysql/bin to the last line

Note:

Effective time: effective when a new terminal is opened with the same user, or manual source ~ / .bash_profile takes effect

Effective period: permanently valid

Effective scope: valid for current users only

If there is no ~ / .profile file, you can edit the ~ / .profile file or create a new one

Linux environment variable configuration method 4: vim / etc/bashrc

The method is to modify the system configuration, requiring administrator privileges (such as root) or write permissions to the file:

# if the / etc/bashrc file is not editable, you need to modify it to editable chmod-v Ubunw / etc/bashrcvim / etc/bashrc# and add export PATH=$PATH:/home/uusama/mysql/bin to the last line

Note:

Effective time: the newly opened terminal takes effect, or the manual source / etc/bashrc takes effect

Effective period: permanently valid

Effective scope: valid for all users

Linux environment variable configuration method 5: vim / etc/profile

This method modifies the system configuration and requires administrator permissions or write permissions to the file, similar to vim / etc/bashrc:

# if the / etc/profile file is not editable, you need to modify it to editable chmod-v Ubunw / etc/profilevim / etc/profile# and add export PATH=$PATH:/home/uusama/mysql/bin to the last line

Note:

Effective time: the newly opened terminal takes effect, or the manual source / etc/profile takes effect

Effective period: permanently valid

Effective scope: valid for all users

Linux environment variable configuration method 6: vim / etc/environment

The method is to modify the system environment configuration file, which requires administrator privileges or write permissions to the file:

# if the / etc/bashrc file is not editable, you need to modify it to editable chmod-v Ubunw / etc/environmentvim / etc/profile# and add export PATH=$PATH:/home/uusama/mysql/bin to the last line

Note:

Effective time: the newly opened terminal takes effect, or the manual source / etc/environment takes effect

Effective period: permanently valid

Effective scope: valid for all users

Analysis of the loading principle of Linux Environment variables

The various configuration methods for environment variables are listed above, so how does Linux load these configurations? In what order is it loaded?

A specific loading order can cause the definition of an environment variable with the same name to be overwritten or ineffective.

Classification of environmental variables

Environment variables can be simply divided into user-defined environment variables and system-level environment variables.

User-level environment variable definition file: ~ / .bashrc, ~ / .bash_profile

System-level environment variable definition files: / etc/bashrc, / etc/bash_profile, / etc/environment

In addition, in the user environment variables, the system will first read the ~ / .bashrc file, if there is no such file, read ~ / .bash_login, if there is no such file, read ~ / .profile, and then read ~ / .bashrc according to the contents of these files.

A method for testing the loading order of Linux environment variables

To test the loading order of environment variables for different files, we define the same environment variable UU_ORDER in the first line of each environment variable definition file, whose value is its own value concatenated with the current file name.

The files that need to be modified are as follows:

/ etc/environment

/ etc/profile

/ etc/profile.d/test.sh, create a new file, no folder to skip

/ etc/bashrc, or / etc/bash.bashrc

~ / .bash_profile, or ~ / .profile

~ / .bashrc

Add the following code to the first line of each file, and modify the colon to the absolute file name of the current file accordingly.

Export UU_ORDER= "$UU_ORDER:~/.bash_profile"

Save after modification, open a new window, and then echo $UU_ORDER to observe the value of the variable:

Uusama@ubuntu:~$ echo $UU_ORDER$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

It can be inferred that the order in which Linux loads environment variables is as follows:

/ etc/environment

/ etc/profile

/ etc/bash.bashrc

/ etc/profile.d/test.sh

~ / .profile

~ / .bashrc

Detailed explanation of loading Linux environment variable file

From the above tests, it is easy to conclude that the order in which Linux loads environment variables is as follows:

System environment variables-> user-defined environment variables

/ etc/environment-> / etc/profile-> ~ / .profile

Open the / etc/profile file and you will find that the / etc/bash.bashrc file is loaded in the code of the file, then check the .sh file in the / etc/profile.d/ directory and load it.

# / etc/profile: system-wide .profile file for the Bourne shell (sh (1)) # and Bourne compatible shells (bash (1), ksh (1), ash (1),...). If ["$PS1"]; then if ["$BASH"] & & ["$BASH"! = "/ bin/sh"]; then # The file bash.bashrc already sets the default PS1. # PS1='\ h:\ w\ $'if [- f / etc/bash.bashrc]; then. / etc/bash.bashrc fi else if ["``id-u`"-eq 0]; then PS1='# 'else PS1='$' fi fifiif [- d / etc/profile.d]; then for i in / etc/profile.d/*.sh; do if [- r $I]; then. $ifi done unset ifi

Secondly, open the ~ / .profile file, and you will find that the ~ / .bashrc file is loaded in the file.

# if running bashif [- n "$BASH_VERSION"]; then # include .bashrc if it exists if [- f "$HOME/.bashrc"]; then. "$HOME/.bashrc" fifi# set PATH so it includes user's private bin directoriesPATH= "$HOME/bin:$HOME/.local/bin:$PATH" what is a Linux system Linux is a free to use and free dissemination of UNIX-like operating system, is a POSIX-based multi-user, multi-tasking, support multi-threading and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above is how to configure environment variables in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report