When customizing the installation of software, you often need to configure environment variables. The following lists various methods for configuring environment variables. The environment for all the examples below is as follows:
Linux reads environment variables Methods for reading environment variables:
The effects of executing these two commands are as follows uusama@ubuntu:~$ export declare -x HOME="/home/uusama" declare -x LANG="en_US.UTF-8" declare -x LANGUAGE="en_US:" declare -x LESSCLOSE="/usr/bin/lesspipe %s %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 search path for running commands, with colons separating different paths. Double quotes can be added or not when using export definitions. Linux environment variable configuration method 1: export PATH Use the export command to directly modify the value of PATH and configure MySQL to enter the environment variable: export PATH=/home/uusama/mysql/bin:$PATH # Or put PATH in front export PATH=$PATH:/home/uusama/mysql/bin Note:
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:
Linux environment variable configuration method three: vim ~/.bash_profile Similar to modifying the ~/.bashrc file, just add the new path at the end of the file: vim ~/.bash_profile # Add export PATH=$PATH:/home/uusama/mysql/bin to the last line Note:
Linux environment variable configuration method 4: vim /etc/bashrc This method is to modify the system configuration, which requires administrator privileges (such as root) or write privileges to the file: # If the /etc/bashrc file is not editable, you need to modify it to be editable chmod -v u+w /etc/bashrc vim /etc/bashrc # Add export PATH=$PATH:/home/uusama/mysql/bin to the last line Note:
Linux environment variable configuration method 5: vim /etc/profile This method modifies the system configuration and requires administrator privileges or write privileges to the file, similar to vim /etc/bashrc: # If the /etc/profile file is not editable, you need to modify it to be editable chmod -v u+w /etc/profile vim /etc/profile # Add export PATH=$PATH:/home/uusama/mysql/bin to the last line Note:
Linux environment variable configuration method six: vim /etc/environment This method is to modify the system environment configuration file, which requires administrator privileges or write privileges to the file: # If the /etc/bashrc file is not editable, you need to modify it to be editable chmod -v u+w /etc/environment vim /etc/profile # Add export PATH=$PATH:/home/uusama/mysql/bin to the last line Note:
Analysis of Linux environment variable loading principle The above lists various configuration methods of environment variables, so how does Linux load these configurations? In what order are they loaded? A specific loading order can cause environment variable definitions with the same name to be overwritten or ineffective. Classification of environment variables Environment variables can be simply divided into user-defined environment variables and system-level environment variables.
In addition, in the user environment variables, the system will first read the ~/.bash_profile file. If the file does not exist, it will read ~/.bash_login. If the file does not exist either, it will read ~/.profile, and then read ~/.bashrc based on the contents of these files. How to test the Linux environment variable loading order In order to test the order in which environment variables are loaded for different files, we define the same environment variable UU_ORDER in the first line of each environment variable definition file. The value of this variable is its own value concatenated with the current file name. The files that need to be modified are as follows:
Add the following code to the first line of each file, and modify the content after the colon to the absolute file name of the current file. After modification, save, 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:
Detailed explanation of Linux environment variable file loading From the above test, it can be easily concluded that the order in which Linux loads environment variables is as follows: System environment variables -> User defined environment variables Open the /etc/profile file and you will find that the code in this file will load the /etc/bash.bashrc file, then check and load the .sh file in the /etc/profile.d/ directory. # /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 fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi Secondly, open the ~/.profile file and you will find that the ~/.bashrc file is loaded in it. # if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin directories PATH="$HOME/bin:$HOME/.local/bin:$PATH" It is not difficult to find from the code in the ~/.profile file that the /.profile file is only read once when the user logs in, while /.bashrc is read once each time the Shell script is run. Some tips You can customize an environment variable file, for example, define uusama.profile under a certain project, use export to define a series of variables in this file, and then add: source uusama.profile after the ~/.profile file. In this way, you can use the series of variables you defined in the Shell script every time you log in. You can also use the alias command to define some command aliases, such as alias rm="rm -i" (double quotes are required), and add this code to ~/.profile. This way, every time you use the rm command, it is equivalent to using the rm -i command, which is very convenient. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Element tree control integrates a drop-down menu with icons (tree+dropdown+input)
>>: How to import Excel files into MySQL database
Table of contents 1. Analysis of key source code ...
Table of contents Install Tomcat Download Tomcat ...
The current requirement is: there is a file uploa...
<br />I just saw the newly revamped ChinaUI....
Installation Script Ubuntu / CentOS There seems t...
Cleanly uninstall MySQL. Personally tested, this ...
Table of contents compose function Array.prototyp...
Three functions: 1. Automatic vertical centering o...
The previous article introduced how Vue can reali...
Table of contents Preface think Library directory...
Table of contents Overview 1. RangeError 2. Refer...
Table of contents 1. Using Set()+Array.from() 2. ...
This is what happened. Today I was playing with G...
The plugin is installed in the Firefox browser. T...
There are two common loading icons on the web, on...