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:
The methods to read environment variables are listed below:
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 (").
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:
Don't forget to add the original configuration ($PATH
) to the configured environment variable to avoid overwriting the original configuration.
Modify the ~/.bashrc file in user directory to configure:
vim ~/.bashrc
# Add in the last line
export PATH=$PATH:/home/uusama/mysql/bin
Note:
If the PATH definition is overwritten when a subsequent environment variable loads the file, it may not take effect.
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:
If the ~/.bash_profile file does not exist, edit the ~/.profile file or create a new one.
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:
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:
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:
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.
Environment variables can be divided into user-defined and system-level environment variables.
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.
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
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.
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.
1,044 posts | 257 followers
Followvangie - January 23, 2019
Alibaba Clouder - September 30, 2018
Alibaba Clouder - September 29, 2018
Alex - January 22, 2020
Alibaba Clouder - April 12, 2021
Alibaba Clouder - May 23, 2018
1,044 posts | 257 followers
FollowAlibaba Cloud Linux is a free-to-use, native operating system that provides a stable, reliable, and high-performance environment for your applications.
Learn MoreExplore Web Hosting solutions that can power your personal website or empower your online business.
Learn MoreAnalyticDB for MySQL is a real-time data warehousing service that can process petabytes of data with high concurrency and low latency.
Learn MoreA low-code development platform to make work easier
Learn MoreMore Posts by Alibaba Cloud Community