Table of Contents
- Configuring Linux Environment Variables
- Accessing Environment Variables in Linux
- Linux Environment Variable Configuration Method 1: export PATH
- Linux Environment Variable Configuration Method 2: vim ~/.bashrc
- Linux Environment Variable Configuration Method 3: vim ~/.bash_profile
- Linux Environment Variable Configuration Method 4: vim /etc/bashrc
- Linux Environment Variable Configuration Method 5: vim /etc/profile
- Linux Environment Variable Configuration Method 6: vim /etc/environment
- Analysis of the Linux Environment Variable Loading Mechanism
- Classification of Environment Variables
- Method for Testing the Loading Order of Linux Environment Variables
- Detailed Explanation of Loading Linux Environment Variable Files
- Tips
Configuring Linux Environment Variables
During the customization of software installations, the frequent need arises to set up environment variables. The following enumerates diverse methods for configuring environment variables.
Example:
- Operating System: Ubuntu 14.0
- Username: spoto
- Path for configuring MySQL environment variables: /home/spoto/mysql/bin
Accessing Environment Variables in Linux
To retrieve environment variables, employ the following methods:
- Utilize the 'export' command to reveal all system-defined environment variables.
- Employ the 'echo $PATH' command to display the current value of the PATH environment variable.
Executing these commands yields the following outcomes:
spoto:~export
declare -x HOME="/home/spoto"
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="spoto"
declare -x MAIL="/var/mail/spoto"
declare -x PATH="/home/spoto/bin:/home/spoto/.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="spoto"
spoto:~ echo $PATH
/home/spoto/bin:/home/spoto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The PATH variable delineates the search paths for executing commands. It separates different paths with colons (:), and when defined using export, double quotes may or may not be included.
Linux Environment Variable Configuration Method 1: export PATH
Modify the value of PATH directly using the export command. Here is the method for configuring MySQL to enter the environment variable:
export PATH=/home/spoto/mysql/bin:PATH
# Or put the PATH in the front.
export PATH=PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Immediate
- Validity Period: Applicable for the current terminal session; becomes invalid after closing the window
- Scope of Application: Effective exclusively for the current user
- While configuring environment variables, it is crucial to include the original settings, specifically the $PATH segment, to prevent overwriting existing configurations.
Linux Environment Variable Configuration Method 2: vim ~/.bashrc
Configure by modifying the ~/.bashrc file located in the user's directory:
vim ~/.bashrc
# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bashrc.'
- Duration of Effect: Permanently valid
- Scope of Application: Applicable only to the current user
- If subsequent environment variable loading files override the PATH definition, it may not take effect.
Linux Environment Variable Configuration Method 3: vim ~/.bash_profile
Similar to modifying the ~/.bashrc file, append the new path at the end of the file:
vim ~/.bash_profile
# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bash_profile.'
- Duration of Effect: Permanently valid
- Scope of Application: Applicable only to the current user
- 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
This method involves modifying system configurations and requires administrator privileges (such as root) or write permissions for 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 this to the last line
export PATH=$PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/bashrc.
- Duration of Effect: Permanently valid
- Scope of Application: Effective for all users
Linux Environment Variable Configuration Method 5: vim /etc/profile
This method involves modifying system configurations and requires administrator privileges or write permissions for 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 this to the last line
export PATH=$PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/profile.
- Duration of Effect: Permanently valid
- Scope of Application: Effective for all users
Linux Environment Variable Configuration Method 6: vim /etc/environment
This method involves modifying the system's environment configuration file and requires administrator privileges or write permissions for 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 this to the last line
export PATH=$PATH:/home/spoto/mysql/bin
Notes:
- Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/environment.
- Duration of Effect: Permanently valid
- Scope of Application: Effective for all users
Analysis of the Linux Environment Variable Loading Mechanism
The various configuration methods for environment variables have been outlined above. How does Linux load these configurations, and in what order?
Specific loading sequences can result in the overriding or ineffectiveness of environment variable definitions with identical names.
Classification of Environment Variables
Environment variables can be broadly categorized into user-defined environment variables and system-level environment variables.
- User-level environment variable definition files: ~/.bashrc, ~/.profile (on some systems: ~/.bash_profile)
- System-level environment variable definition files: /etc/bashrc, /etc/profile (on some systems: /etc/bash_profile), /etc/environment
Method for Testing the Loading Order of Linux Environment Variables
To test the loading order of environment variables in different files, we will define the environment variable UU_ORDER at the beginning of each environment variable definition file. The value of this variable will be the concatenation of its own value with the current file name. The files to be modified are as follows:
- /etc/environment
- /etc/profile
- /etc/bash.bashrc
- /etc/profile.d/test.sh
- ~/.profile
- ~/.bashrc
Add the following code as the first line in each file, and adjust the content after the colon to the absolute file name of the current file accordingly.
export UU_ORDER="$UU_ORDER:~/.bash_profile"
After making the modifications, save the changes. Open a new terminal window and observe the value of the variable by executing 'echo $UU_ORDER':
spoto:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc
The sequence for loading environment variables in Linux can be inferred as follows:
- /etc/environment
- /etc/profile
- /etc/bash.bashrc
- /etc/profile.d/test.sh
- ~/.profile
- ~/.bashrc
Detailed Explanation of Loading Linux Environment Variable Files
From the above test, it is evident that Linux loads environment variables in the following sequence:
- System environment variables -> User-defined environment variables
- /etc/environment -> /etc/profile -> ~/.profile
Upon opening the /etc/profile file, you will notice that it loads the /etc/bash.bashrc file. It then checks and loads .sh files in the /etc/profile.d/ directory.
# /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
Subsequently, upon inspecting the ~/.profile file, it becomes apparent that this file includes the loading of the ~/.bashrc 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"
Tips
You can customize an environment variable file, such as defining 'spoto.profile' within a specific project. In this file, use 'export' to define a series of variables. Then, append 'source spoto.profile' at the end of the ~/.profile file. This allows you to access your personally defined set of variables in Shell scripts every time you log in.
Alternatively, you can use the 'alias' command to define aliases for specific commands. For instance, 'alias rm="rm -i"' (double quotes are necessary). Incorporating this code into ~/.profile ensures that every time you use the 'rm' command, it is essentially executing 'rm -i,' offering a convenient and efficient solution.