×
Community Blog A Guide on Environment Variable Configuration in Linux

A Guide on Environment Variable Configuration in Linux

This is a guide on environment variable configuration for the Linux operating system.

Environment Variable Configuration in Linux

We often need to configure environment variables when we customize the software installation. The following sections describe various configuration methods for environment variables.

The environment in the following examples is listed below:

  • System: Ubuntu 14.0
  • User Name: uusama
  • Path of MySQL Environment Variables: /home/uusama/mysql/bin

Read Environment Variables in Linux

The methods to read environment variables are listed below:

  • The export command displays all currently defined environment variables.
  • The echo $PATH command outputs the value of current PATH environment variables.

The effects of these two commands are listed below:

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. Different paths are separated with colons (:). You can use the export definition with or without double quotation marks (").

Method 1 of Environment Variable Configuration in Linux: export PATH

Use the export command to modify the value of PATH and configure the method of accessing the environment variables for MySQL:

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

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

Note:

  • Effective Time: It takes effect immediately.
  • Effective Period: The current terminal is valid and will be invalid after the window is closed.
  • Scope: It only applies to the current user.

Don't forget to add the original configuration ($PATH) to the configured environment variable to avoid overwriting the original configuration.

Method 2 of Environment Variable Configuration in Linux: vim ~/.bashrc

Modify the ~/.bashrc file in user directory to configure:

vim ~/.bashrc

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

Note:

  • Effective Time: The time when the same user opens a new terminal or when a user runs source ~/.bashrc manually
  • Effective Period: Permanently valid
  • Scope: It only applies to the current user.

If the PATH definition is overwritten when a subsequent environment variable loads the file, it may not take effect.

Method 3 of Environment Variable Configuration in Linux: vim ~/.bash_profile

This is similar to modifying the ~/.bashrc file; you only need to add a new path at the end of the file:

vim ~/.bash_profile

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

Note:

  • Effective Time: The time when the same user opens a new terminal or when a user runs source ~/.bash_profile manually
  • Effective Period: Permanently valid
  • Scope: It only applies to the current user.

If the ~/.bash_profile file does not exist, edit the ~/.profile file or create a new one.

Method 4 of Environment Variable Configuration in Linux: vim /etc/bashrc

This method modifies the system configurations and requires administrator permissions (such as root) or writing permission for the file:

# If the /etc/bashrc file cannot be edited, it needs to be changed to the editable state.
chmod -v u+w /etc/bashrc

vim /etc/bashrc

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

Note:

  • Effective Time: It takes effect when opening a new terminal or running source /etc/bashrc manually.
  • Effective Period: Permanently valid
  • Scope: It applies to all users.

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

This method modifies system configurations and requires administrator permissions or writing permission for the file. It is similar to vim/etc/bashrc:

# If the /etc/profile file cannot be edited, it needs to be changed to the editable state.
chmod -v u+w /etc/profile

vim /etc/profile

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

Note:

  • Effective Time: It takes effect when opening a new terminal or running source /etc/profile manually.
  • Effective Period: Permanently valid
  • Scope: It applies to all users.

Method 6 of Environment Variable Configuration in Linux: vim/etc/environment

This method modifies system environment configuration files and requires administrator permissions or writing permission for the file:

# If the /etc/bashrc file cannot be edited, it needs to be changed to the editable state.
chmod -v u+w /etc/environment

vim /etc/profile

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

Note:

  • Effective Time: It takes effect when opening a new terminal or manually running source /etc/environment.
  • Effective Period: Permanently valid
  • Scope: It applies to all users.

Explanation of Environment Variables Loading in Linux

The sections above have described various configuration methods of environment variables, but how does Linux load these configurations? What is the loading order?

The particular loading order causes environment variable definitions with the same name to be overwritten or invalid.

Classification of Environment Variables

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

  • Definition Files of User-Level Environment Variables: ~/.bashrc and ~/.profile (for some systems: ~/.bash_profile)
  • Definition Files of System-Level Environment Variables: /etc/bashrc, /etc/profile (for some systems: /etc/bash_profile), and /etc/environment

In addition, the system reads the ~/.bash_profile (or ~/.profile) file first for user-level environment variables. If there is no such file, read ~/.bash_login and then read ~/.bashrc based on the content of these files.

Method of Testing the Environment Variable Loading Order in Linux

We define the same environment variable UU_ORDER in the first line of each environment variable definition file to test the loading order of environment variables in different files. The value of this variable is its own value plus the current file name.

The files that need to be modified are listed below:

/etc/environment
/etc/profile
/etc/profile.d/test.sh. Create a file. If no folder exists, skip this file.
/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 content after the colon to the absolute file name of the current file accordingly.

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

Complete the changes and save them. Then, open a new window and run 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

The order of loading environment variables in Linux can be inferred like this:

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

A Detailed Explanation of Environment Variable File Loading in Linux

From the tests above, it can be concluded that the order of Linux loading environment variables is:

System environment variables -> user-defined environment variables
/etc/environment -> /etc/profile -> ~/.profile

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

Open the ~/.profile file again, and you will see that the ~/.bashrc file is loaded in this 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"

According to the code of the ~/.profile file, the /.profile file only reads when the user logs in, while /.bashrc is read once every time the Shell script runs.

Tips

You can customize an environment variable file, such as defining uusama.profile under a project. Use export to define a series of variables in the file, and then add sourc uusama.profile after the ~/.profile file. By doing so, you can use a series of customized variables in the Shell script every time you log in.

You can also use the alias command to define the alias of some commands. For example, alias rm = "rm -i". (The double quotation marks are required.) Add the code to ~/.profile. It is convenient each time you run the rm command because it is like running the rm -i command.

This article was originally published on the Programmer Bai Nannan official WeChat account.

Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 0 0
Share on

Alibaba Cloud Community

1,044 posts | 257 followers

You may also like

Comments