linux reads environment variables

Environment variables often need to be configured during custom software installation. The following lists various configuration methods for environment variables.

The environment of all the examples below is described as follows:

Linux reads environment variables

How to read environment variables:

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

The effects of 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 the command. Colon: separate different paths. You can use double quotation marks or no quotation marks when using export definition.

Linux environment variable configuration method 1: export PATH

Use the export command to directly modify the PATH value and configure MySQL's method of entering environment variables:

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

#  Or put the PATH in front
export PATH=PATH:/home/uusama/mysql/bin

matters needing attention:

  • Effective time: immediately
  • Effective period: the current terminal is valid and invalid after the window is closed
  • Effective range: only valid for the current user
  • Don't forget to add the original configuration, that is, $PATH, to the configured environment variables 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

#  Add on the last line
export PATH=$PATH:/home/uusama/mysql/bin

matters needing attention:

  • Effective time: it takes effect when a new terminal is opened with the same user, or it takes effect manually with source ~/.bashrc
  • Effective period: permanent
  • Effective range: only valid for the current user
  • If a subsequent environment variable loading file overwrites 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

#  Add on the 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 manually source ~/.bash_profile validation
  • Effective period: permanent
  • Effective range: only valid for the current user
  • If there is no ~ /. bash_profile file, you can edit ~ /. profile file or create a new one

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

This method is to modify the system configuration, which requires administrator permission (such as root) or write permission to the file:

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

vim /etc/bashrc

#  Add on the last line
export PATH=$PATH:/home/uusama/mysql/bin

matters needing attention:

  • Effective time: the newly opened terminal takes effect, or the manual source /etc/bashrc takes effect
  • Effective period: permanent
  • Effective range: valid for all users

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

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

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

vim /etc/profile

#  Add on the last line
export PATH=$PATH:/home/uusama/mysql/bin

matters needing attention:

  • Effective time: the newly opened terminal takes effect, or the source /etc/profile takes effect manually
  • Effective period: permanent
  • Effective range: valid for all users

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

This 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 not editable and needs to be modified to be editable
chmod -v u+w /etc/environment

vim /etc/profile

#  Add on the last line
export PATH=$PATH:/home/uusama/mysql/bin

matters needing attention:

  • Effective time: the newly opened terminal takes effect, or the source /etc/environment takes effect manually
  • Effective period: permanent
  • Effective range: valid for all users

Analysis of Linux environment variable loading principle

Various configuration methods of environment variables are listed above. How does Linux load these configurations? In what order is it loaded?

The specific loading order will cause the environment variable definition with the same name to be overwritten or ineffective.

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, ~ /. Profile (some systems are: ~ /. bash_profile)
  • System level environment variable definition files: / etc/bashrc, / etc / profile (some systems are: / etc/bash_profile), / etc/environment

In addition, in the user environment variable, the system will first read the ~ /. bash_profile (or ~ /. Profile) file. If there is no such file, read ~ /. bash_login, and then read ~ /. bashrc according to the contents of these files.

Method for testing loading order of Linux environment variables

In order to test the loading order of environment variables in 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, connected with the current file name.

The documents 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 accordingly.

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

That's how Linux should learn (2nd Edition)

Author: Liu Trent

Dangdang
  purchase

↑   ↑   Rush purchase within a limited time! Only 48.1 yuan   ↑   ↑

Save after modification, open a new window, and echo $UU_ORDER to observe the value of the variable:

uusama@ubuntu:~echoUU_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:

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

Linux environment variable file loading details

From the above tests, it can be easily concluded that the sequence of Linux loading environment variables is as follows:

System environment variable - > user defined environment variable / etc / environment - > / etc / profile - > ~ /. Profile

Open the / etc/profile file, you will find that the code of the file will load the / etc/bash.bashrc file, and 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

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

# 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 the code from the ~ /. profile file. The /. profile file is read only once when the user logs in, while /. bashrc will read once every time the Shell script is run.

Some tips

You can customize an environment variable file. For example, define uusama.profile under a project. In this file, use export to define a series of variables, and then add: sourc uusama.profile after 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 some command aliases, such as alias rm="rm -i" (double quotation marks must be used), and add this code to the ~ /. profile, so that each time you use the rm command, it is equivalent to using the rm -i command, which is very convenient

Tags: Linux shell

Posted on Sun, 05 Dec 2021 03:06:41 -0500 by srboj