Summary of Linux environment variable configuration

01. Linux Environment Variable Configuration

When you customize software installation, you often need to configure environment variables. The following list shows how to configure environment variables.

The environment for all the examples below is illustrated as follows:

System: Ubuntu 14.0
User name: uusama
MySQL environment variable path needs to be configured: /home/uusama/mysql/bin

02. Linux Read Environment Variables

How to read environment variables:

The export command displays all environment variables currently defined by the system
The echo $PATH command outputs the value of the current PATH environment variable
These two commands execute 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 to run the command, with a colon: split different paths, and use the export definition with or without double quotes.

03. Linux environment variable configuration method 1: export PATH

Configure how MySQL enters environment variables by directly modifying the PATH value using the export command:

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

Or put PATH in front

export PATH=$PATH:/home/uusama/mysql/bin
Matters needing attention:

Effective time: Effective immediately
Effective Duration: The current terminal is valid, not after the window is closed
Effective range: only valid for current user
Don't forget to add the original configuration, the $PATH section, to the configured environment variables to avoid overwriting the original configuration

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

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

vim ~/.bashrc

Add on last line

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

Matters needing attention:

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 user
If a subsequent environment variable load file overrides the PATH definition, it may not take effect

05. Configuration method of Linux environment variables 3: vim ~/.bash_profile

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

vim ~/.bash_profile

Add on last line

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

Matters needing attention:

Effective Time: Effective when opening a new terminal with the same user, or manually source ~/.bash_profile takes effect
Effective Period: Permanent
Effective range: only valid for current user
If there is no /.bash_profile file, you can edit the /.profile file or create a new one

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

This method modifies the system configuration and requires administrator privileges (such as root) or write access to the file:

If the / etc/bashrc file is not editable, it needs to be modified to be editable

chmod -v u+w /etc/bashrc
vim /etc/bashrc

Add on last line

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

Matters needing attention:

Effective time: new terminal or manual source/etc/bashrc
Effective Period: Permanent
Effective range: valid for all users

07. Configuration method of Linux environment variables 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 the/etc/profile file is not editable, it needs to be modified to be editable

chmod -v u+w /etc/profile

vim /etc/profile

Add on last line

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

Matters needing attention:

Effective time: new terminal or manual source/etc/profile takes effect
Effective Period: Permanent
Effective range: valid for all users

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

This method modifies the system environment profile, requiring administrator or write access to the file:

If the / etc/bashrc file is not editable, it needs to be modified to be editable

chmod -v u+w /etc/environment
vim /etc/profile

Add on last line

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

Matters needing attention:

Effective time: new terminal or manual source/etc/environment will take effect
Effective Period: Permanent
Effective range: valid for all users

09. Analysis of Loading Principle of Linux Environment Variables

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

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

10. Classification of environmental variables

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

User-level environment variable definition files: /.bashrc, /.profile (partial system: ~/.bash_profile)
System-level environment variable definition files: /etc/bashrc, /etc/profile (partial system: /etc/bash_profile), /etc/environment
In addition, in user environment variables, the system reads the /.bash_profile (or /.profile) file first, or /.bash_login if it is not, and then /.bashrc based on the contents of these files.

11. Ways to test the loading order of Linux environment variables

To test the loading order of environment variables for each different file, we define the same environment variable UU_in the first line of each environment variable definition file ORDER, the value of which is the current file name connected to its own value.

The following files need to be modified:

/etc/environment
/etc/profile
/etc/profile.d/test.sh,New file, no folder to skip
/etc/bashrc,perhaps/etc/bash.bashrc
~/.bash_profile,perhaps~/.profile
~/.bashrc

Add the following code to the first line 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 after modification, open a new window, and echo $UU_ The value of the ORDER observation 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

12. Linux Environment Variable File Loading Details
From the above tests, it is easy to see that Linux loads environment variables in the following order:

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
  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'll see 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 see the code from the ~/.profile file, which is read only once when the user logs in, and /.bashrc, which is read every time the Shell script is run.

13. Some tips

You can customize an environment variable file, such as defining uusama.profile under a project, in which you define a series of variables using export, and then add: sourc uusama.profile after the ~/.profile file, so that you can use a series of variables you define each time you log on to the Shell script.

You can also use the alias command to define alias names for some commands, such as alias rm="rm-i" (double quotation marks must be used), and add this code to ~/.profile, so that each time you use the RM command, it is as convenient as using the rm-i command.

Tags: Linux Ubuntu server

Posted on Sun, 05 Dec 2021 19:26:03 -0500 by andco