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-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to configure environment variables in Linux, which is very detailed and has a certain reference value. Friends who are interested must read it!

The environment for all of the following examples is as follows:

System: Ubuntu 14.0 user name: uusama needs to configure MySQL environment variable path: / home/uusama/mysql/binLinux read environment variable path: export command displays all current system-defined environment variables echo $PATH command outputs the value of the current PATH environment variable the effect of the 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 where the PATH variable defines the lookup path to run the command With colons: separate paths, with or without double quotes when using the export definition. Linux environment variable configuration method 1: export PATH uses the export command to directly modify the value of PATH, 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 period: current terminal is valid Invalid scope after window closes: don't forget to add the original configuration, that is, the $PATH section, to the environment variables that are only valid for the current user. 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 notes on the last line: effective time: effective when a new terminal is opened with the same user Or manual source ~ / .bashrc effective period: permanent effective range: only valid for current users. If a subsequent environment variable loading file overrides the PATH definition, it may not take effect. Linux environment variable configuration method 3: vim ~ / .bashrc file is similar to modifying ~ / .bashrc file. It is also necessary to add a new path to the end of the file: vim ~ / .bash_profile# add export PATH=$PATH:/home/uusama/mysql/bin note on the last line: effective time: effective time: effective when a new terminal is opened with the same user, or manual source ~ / .bash_profile effective period: permanent valid range: valid only for current users if there is no ~ / .bash_profile file You can edit the ~ / .profile file or create a new Linux environment variable configuration method 4: vim / etc/bashrc this method modifies the system configuration and requires administrator permissions (such as root) or write permissions to the file: # if the / etc/bashrc file is not editable Need to be modified to editable chmod-v UCPW / etc/bashrcvim / etc/bashrc# plus export PATH=$PATH:/home/uusama/mysql/bin note on the last line: effective time: newly opened terminal takes effect, or manual source / etc/bashrc effective period: permanent effective range: effective Linux environment variable configuration method for all users 5: vim / etc/profile this method modifies the system configuration Administrator permission or write permission to the file is required, similar to vim / etc/bashrc: # if the / etc/profile file is not editable, it needs to be modified to editable chmod-v UCPW / etc/profilevim / etc/profile# with export PATH=$PATH:/home/uusama/mysql/bin notes on the last line: effective time: the newly opened terminal takes effect. Or manual source / etc/profile effective period: permanent effective scope: valid for all users Linux environment variable configuration method 6: vim / etc/environment this method is to modify the system environment configuration file, requires administrator permission or write permission to the file: # if the / etc/bashrc file is not editable Need to be modified to editable chmod-v UCPW / etc/environmentvim / etc/profile# in the last line plus export PATH=$PATH:/home/uusama/mysql/bin note: effective time: newly opened terminal takes effect, or manual source / etc/environment effective period: permanent effective range: effective Linux environment variable loading principle for all users various configuration methods 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. The classification of environment variables can be simply divided into user-defined environment variables and system-level environment variables. User-level environment variable definition files: ~ / .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 ~ / .bash_profile file, if there is no such file, read ~ / .profile, 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, there is 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 change the contents after the colon to the absolute file name of the current file accordingly. Save export UU_ORDER= "$UU_ORDER:~/.bash_profile" after modification and open a new window Then echo $UU_ORDER observes 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 can infer that the order in which Linux loads the environment variables is as follows: / etc/environment/etc/profile/etc/bash.bashrc/etc/profile.d/test.sh~/.profile~/.bashrcLinux environment variable file loading details

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 / etc/profile file you will find that the file's code will load the / etc/bash.bashrc 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"]; thenif ["$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 fielse if ["``id-u`"-eq 0]; then PS1='# 'else PS1='$' fififiif [- d / etc/profile.d]; thenfor i in / etc/profile.d/*.sh; do if [- r $I]; then. $i fidoneunset ifi then 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" is not difficult to find from the code in the ~ / .profile file, which is read only once when the user logs in, and / .bashrc is read every time the Shell script is run. Some tips can customize an environment variable file, such as defining uusama.profile under a project, using export to define a series of variables in this file, and then adding: sourc uusama.profile to the ~ / .profile file, so that you can use a series of variables defined by yourself in the Shell script every time you log in. You can also use the alias command to define aliases for some commands, such as alias rm= "rm-I" (double quotes must be), and add this code to ~ / .profile, so that every time you use the rm command, it is equivalent to using the rm-I command, which is very convenient.

These are all the contents of the article "how to configure environment variables in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, 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

Development

Wechat

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

12
Report