what line must you add to /etc/profile to make sure
A Guide on Environment Variable Configuration in Linux
Environs Variable Configuration in Linux
We often need to configure surround variables when we customize the software installation. The following sections depict diverse configuration methods for environment variables.
The surround 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 beneath:
- The export command displays all currently defined environment variables.
- The
echo $PATH
command outputs the value of electric current PATH environment variables.
The effects of these two commands are listed beneath:
uusama@ubuntu:~$ export declare -x Home="/home/uusama" declare -x LANG="en_US.UTF-eight" declare -x LANGUAGE="en_US:" declare -x LESSCLOSE="/usr/bin/lesspipe %due south %s" declare -x LESSOPEN="| /usr/bin/lesspipe %southward" declare -10 LOGNAME="uusama" declare -x Post="/var/postal service/uusama" declare -x PATH="/dwelling/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:~$ repeat $PATH /home/uusama/bin:/domicile/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 tin apply 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 forepart export PATH=$PATH:/home/uusama/mysql/bin
Notation:
- Effective Fourth dimension: Information technology takes effect immediately.
- Effective Period: The current last is valid and will be invalid after the window is closed.
- Scope: It merely 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 Surround Variable Configuration in Linux: vim ~/.bashrc
Modify the ~/.bashrc file in user directory to configure:
vim ~/.bashrc # Add in the last line consign PATH=$PATH:/domicile/uusama/mysql/bin
Notation:
- Effective Time: The fourth dimension when the same user opens a new final or when a user runs source ~/.bashrc manually
- Constructive Menstruum: Permanently valid
- Scope: It only applies to the electric current user.
If the PATH definition is overwritten when a subsequent surround variable loads the file, it may not have effect.
Method three of Surround Variable Configuration in Linux: vim ~/.bash_profile
This is similar to modifying the ~/.bashrc file; you merely 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 Fourth dimension: The time when the aforementioned user opens a new last or when a user runs source ~/.bash_profile manually
- Effective Period: Permanently valid
- Scope: Information technology only applies to the current user.
If the ~/.bash_profile file does non exist, edit the ~/.contour file or create a new one.
Method 4 of Environment Variable Configuration in Linux: vim /etc/bashrc
This method modifies the organization configurations and requires ambassador permissions (such as root) or writing permission for the file:
# If the /etc/bashrc file cannot be edited, information technology needs to be inverse to the editable state. chmod -v u+west /etc/bashrc vim /etc/bashrc # Add together in the last line consign PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: It takes effect when opening a new terminal or running source /etc/bashrc manually.
- Constructive Period: Permanently valid
- Scope: It applies to all users.
Method v of Surroundings Variable Configuration in Linux: vim/etc/profile
This method modifies arrangement 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 exist inverse to the editable country. chmod -5 u+w /etc/profile vim /etc/profile # Add in the terminal line export PATH=$PATH:/dwelling/uusama/mysql/bin
Notation:
- 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 six of Environment Variable Configuration in Linux: vim/etc/environment
This method modifies arrangement surround configuration files and requires administrator permissions or writing permission for the file:
# If the /etc/bashrc file cannot exist edited, it needs to be changed to the editable land. chmod -five u+w /etc/environment vim /etc/profile # Add in the last line consign PATH=$PATH:/home/uusama/mysql/bin
Note:
- Effective Time: Information technology takes effect when opening a new terminal or manually running source /etc/environment.
- Constructive Period: Permanently valid
- Scope: Information technology applies to all users.
Explanation of Environs Variables Loading in Linux
The sections above take described various configuration methods of environment variables, merely how does Linux load these configurations? What is the loading order?
The particular loading order causes environment variable definitions with the same name to exist 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 ~/.contour (for some systems: ~/.bash_profile)
- Definition Files of System-Level Surroundings 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 surroundings variables. If at that place is no such file, read ~/.bash_login and then read ~/.bashrc based on the content of these files.
Method of Testing the Surround Variable Loading Lodge in Linux
We define the same environment variable UU_ORDER in the offset 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 proper 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/fustigate.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 accented file proper noun of the current file accordingly.
export UU_ORDER="$UU_ORDER:~/.bash_profile"
Consummate the changes and save them. Then, open a new window and run echo $UU_ORDER to discover the value of the variable:
uusama@ubuntu:~$ echo $UU_ORDER $UU_ORDER:/etc/environment:/etc/contour:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.contour:~/.bashrc
The order of loading environment variables in Linux tin can exist inferred like this:
/etc/environment /etc/contour /etc/bash.bashrc /etc/profile.d/test.sh ~/.profile ~/.bashrc
A Detailed Caption of Environment Variable File Loading in Linux
From the tests higher up, it can be concluded that the order of Linux loading surround variables is:
System environment variables -> user-defined environment variables /etc/surroundings -> /etc/profile -> ~/.profile
Open the /etc/contour file, and you will find that the code of the file tin load the /etc/fustigate.bashrc file. Then, bank check the .sh file under the /etc/profile.d/ directory and load information technology.
# /etc/profile: system-wide .contour file for the Bourne shell (sh(1)) # and Bourne uniform shells (fustigate(i), ksh(1), ash(ane), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; and so # The file fustigate.bashrc already sets the default PS1. # PS1='\h:\west\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; and so 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 up the ~/.profile file again, and you will see that the ~/.bashrc file is loaded in this file.
# if running bash if [ -n "$BASH_VERSION" ]; and so # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; so . "$HOME/.bashrc" fi fi # set PATH and so it includes user's private bin directories PATH="$Home/bin:$HOME/.local/bin:$PATH"
According to the lawmaking of the ~/.contour file, the /.profile file only reads when the user logs in, while /.bashrc is read one time every fourth dimension the Shell script runs.
Tips
You can customize an surroundings variable file, such as defining uusama.contour under a project. Use export to ascertain a serial of variables in the file, and and then add sourc uusama.profile after the ~/.profile file. By doing so, you can use a serial of customized variables in the Beat out script every time you log in.
Yous tin can besides utilise the alias command to ascertain the alias of some commands. For case, allonym rm = "rm -i"
. (The double quotation marks are required.) Add the lawmaking to ~/.profile. Information technology is convenient each time you run the rm
command considering 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 merely and don't necessarily stand for the official views of Alibaba Deject.
Y'all may also similar
Comments
Related Products
-
Web Hosting Solution
Explore Web Hosting solutions that tin can power your personal website or empower your online business.
Larn More than -
AnalyticDB for MySQL
AnalyticDB for MySQL is a real-fourth dimension data warehousing service that can procedure petabytes of data with high concurrency and low latency.
Learn More -
YiDA Low-lawmaking Development Platform
A depression-lawmaking development platform to make work easier
Learn More than -
Web Hosting
Explore how our Spider web Hosting solutions assistance small and medium sized companies ability their websites and online businesses.
Learn More than
Source: https://www.alibabacloud.com/blog/a-guide-on-environment-variable-configuration-in-linux_598423
0 Response to "what line must you add to /etc/profile to make sure"
Post a Comment