A complete walkthrough of Linux environment variable configuration

  • 2021-01-19 22:33:43
  • OfStack

Linux environment variable configuration

It is often necessary to configure environment variables when installing custom software. The following lists various ways to configure environment variables.

The context for all of the following examples is described as follows:

System: Ubuntu 14.0 User name: uusama Need to configure the MySQL environment variable path: / home uusama/mysql/bin

Linux reads environment variables

How to read environment variables:

The export command displays all the environment variables defined by the current system The echo $PATH command outputs the current value of the PATH environment variable

The effect of these two commands is 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 lookup path for the command to run, with a colon: split between different paths, either in double quotes or without 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 way MySQL enters the environment variable:


export PATH=/home/uusama/mysql/bin:$PATH

#  Or the PATH On the front 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective immediately Effective period: valid for the current terminal, invalid after the window is closed Scope of effect: only valid for the current user Don't forget to include the original configuration, the $PATH section, in the configured environment variables to avoid overwriting the original configuration

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

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


vim ~/.bashrc

#  In the final 1 Line with 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: takes effect when a new terminal is opened with the same user, or manually source ~/.bashrc To take effect Term of validity: permanent Scope of effect: only valid for the current user If there is a subsequent environment variable load file that overrides the PATH definition, it may not take effect

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

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


vim ~/.bash_profile

#  In the final 1 Line with 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: takes effect when a new terminal is opened with the same user, or manual source ~/.bash_profile takes effect Term of validity: permanent Scope of effect: only valid for the current user If the ~/.bash_profile file is not available, you can edit the ~/.profile file or create a new one

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

This method is to modify the system configuration and requires administrator privileges (such as root) or write permissions to the file:


#  if /etc/bashrc The file is not editable and needs to be changed to be editable 
chmod -v u+w /etc/bashrc

vim /etc/bashrc

#  In the final 1 Line with 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective when new terminal is opened, or effective when manual source /etc/bashrc Term of validity: permanent Scope of effect: for all users

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

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


#  if /etc/profile The file is not editable and needs to be changed to be editable 
chmod -v u+w /etc/profile

vim /etc/profile

#  In the final 1 Line with 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective when new terminal is opened, or source /etc/profile manually Term of validity: permanent Scope of effect: for all users

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

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


#  if /etc/bashrc The file is not editable and needs to be changed to be editable 
chmod -v u+w /etc/environment

vim /etc/profile

#  In the final 1 Line with 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective when new terminal is opened, or source /etc/environment manually Term of validity: permanent Scope of effect: for all users

Linux environment variable loading principle analysis

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

A particular load order can cause environment variable definitions of the same name to be overridden or not in effect.

Classification of environment variables

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 variable, the system will first read ~/.bash_profile file, if the file is not available, then read ~/.login, if the file is not available, then read ~/.profile, according to the contents of these files to read ~/.bashrc.

Test the loading order of Linux environment variables

To test the order in which the environment variables are loaded for each of the different files, we define the same environment variable UU_ORDER on line 1 in each environment variable definition file, with its value concatenated to the current file name.

The files that need to be modified are as follows:

/etc/environment /etc/profile / etc profile. d/test. sh, new files, no folder can skip /etc/bashrc, or /etc/bash.bashrc ~/.bash_profile, or ~/.profile ~/.bashrc

Add the following line on line 1 of each file and change the colon to the absolute file name of the current file accordingly.


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

Save it, open a new window, and then echo $UU_ORDER 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

Linux environment variable file load details

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

System environment variable - > User-defined environment variables

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

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


# /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

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


export PATH=/home/uusama/mysql/bin:$PATH

#  Or the PATH On the front 
export PATH=$PATH:/home/uusama/mysql/bin
0

From the code in ~/.profile, /.profile is read only once when the user logs in, whereas /.bashrc is read once each time the Shell script is run.

1 Tips

You can customize one environment variable file, such as under a project uusama.profile In this file, use export to define a series of variables, and then add ~/.profile to the file: sourc uusama.profile , so that you can use your own set of 1 variables 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 are required), and add this code to ~/.profile so that every time you use rm, you are using the same command as rm-i, which is very convenient.

conclusion


Related articles: