Detailed Explanation of Linux Environment Variable Configuration Raiders

  • 2021-07-18 09:41:57
  • OfStack

When installing software by custom, it is often necessary to configure environment variables. The following lists various configuration methods for environment variables.

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

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

Linux Reading Environment Variables

How to read environment variables:

The export command displays all the environment variables currently defined by the 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:~$ 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

Among them, PATH variable defines the search path of running command, which is divided into different paths by colon. When using export definition, double quotation marks can be added or not added.

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 Put it in the front 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: effective immediately Effective period: The current terminal is valid, but it is invalid after the window is closed Valid range: Valid for current user 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

#  At the end 1 Line plus 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective when a new terminal is opened with the same user, or effective manually source ~/. bashrc Effective period: permanently valid Valid range: Valid for current user 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 at the end of the file:


vim ~/.bash_profile

#  At the end 1 Line plus 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: Effective when a new terminal is opened with the same user, or effective manually source ~/. bash_profile Effective period: permanently valid Valid range: Valid for current user only If there is no ~/. bash_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 /etc/bashrc The file is non-editable and needs to be modified to be editable 
chmod -v u+w /etc/bashrc

vim /etc/bashrc

#  At the end 1 Line plus 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: The new terminal takes effect, or source/etc/bashrc takes effect manually Effective period: permanently valid Valid range: Valid for all users

Linux Environment Variable Configuration Method 5: vim/etc/profile

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


#  If /etc/profile The file is non-editable and needs to be modified to be editable 
chmod -v u+w /etc/profile

vim /etc/profile

#  At the end 1 Line plus 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: New terminal effective, or manual source/etc/profile effective Effective period: permanently valid Valid range: 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 permission or write permission to the file:


#  If /etc/bashrc The file is non-editable and needs to be modified to be editable 
chmod -v u+w /etc/environment

vim /etc/profile

#  At the end 1 Line plus 
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

Effective time: The new terminal will take effect, or source/etc/environment will take effect manually Effective period: permanently valid Valid range: Valid for all users

Analysis of Linux Environment Variable Loading Principle

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

A specific loading order will cause environment variable definitions with the same name to be overwritten or not effective.

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 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 the ~ /.bash_profile file, if there is no such file, then read ~ /.bash_login, if there is no such file, then read ~ /.profile, and then read ~ /.bashrc according to the contents of these files.

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 line 1 of each environment variable definition file, the value of which is its own 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 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 change the contents after 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 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 Linux loads environment variables in the following order:

/etc/environment /etc/profile /etc/bash.bashrc /etc/profile.d/test.sh ~/.profile ~/.bashrc

Linux Environment Variable File Loading Detailed Explanation

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 will be loaded in the code of this 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
 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 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"

From the code in the ~/. profile file, it is not difficult to find that the/. profile file is read only once when the user logs in, while/. bashrc is read once every time the Shell script is run.

1 Some Tips

You can customize 1 environment variable file, for example, define uusama. profile under a project, use export to define 1 series variables in this file, and then add: sourc uusama. profile after ~/. profile file, so that you can use your own 1 series variables in Shell script every time you log in.

You can also use the alias command to define an alias for some commands, such as alias rm = "rm-i" (double quotation marks 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.


Related articles: