3 ways to add environment variables to ubuntu system

illustrate:

At work, the software we compile and install ourselves cannot be automatically recognized in the global directory in the system. It can only be run in the relevant directory. For example, if you run the compiled and installed PHP program on the command line, you must /usr/ local/LAMP/php/bin/php file path/test.php so that it can be run (/usr/local/LAMP/php is the software directory compiled and installed by yourself). If you want to run the php file path/test.php anywhere on the terminal command line, then we need to configure the global environment variables ourselves.

The Ubuntu system loads environment variables as follows:
Environment variables can be simply divided into user-defined environment variables and system-level environment variables.
User-level environment variable definition files: home/user/.bashrc, home/user/.profile (some systems are: ~/.bash_profile)
System-level environment variable definition files: /etc/bashrc, /etc/profile (some systems are: ~/.bash_profile) :/etc/bash_profile), /etc/environment
. In addition, in the user environment variables, the system will first read the home/user/.bash_profile (or home/user/.profile) file. If there is no such file, it will read home/user /.bash_login, and then read ~/.bashrc based on the contents of these files.

bash first executes the /etc/profile script, and the /etc/profile script first executes /etc/profile.d/*.sh in sequence. Then bash will execute the .bash_profile (.profile) script in the user's home directory, .bash_profile (.profile ) script will execute the .bashrc script in the user's home directory, and the .bashrc script will execute the /etc/bashrc script. At this point, all environment variables and initialization settings have been loaded. Bash then calls terminfo and inputrc to complete the setting of terminal properties and keyboard mapping.

1. Temporary settings

# 在终端命令行下执行以下命令,这个设置仅限当前终端有效,窗口关闭后无效
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
# 其中PATH变量定义了运行命令的查找路径,以冒号:分割不同的路径,
# 如,/home/yan/share/usr/local/arm/3.4.1/bin 这个就表示一个软件的路径了,
# 多个软件就用:分开,如 /usr/local/LAMP/php/bin:/usr/local/LAMP/mysql/bin

2. Global variables of the currently logged in user

# 修改以下文件
vi ~/.bashrc
# 在该文件末尾添加如下行
export PATH=/home/yan/share/usr/local/arm/3.4.1/bin:$PATH
# 或者,添加多个变量
PATH=$PATH:/usr/local/LAMP/php/bin:/usr/local/LAMP/mysql/bin
export PATH

3. Global variables for all logged in users

# 修改以下文件
sudo vim /etc/profile
# 在最后添加下面这句,保存退出
export PATH=/usr/local/LAMP/mysql/bin:$PATH
# 或者,添加多个变量
PATH=$PATH:/usr/local/LAMP/php/bin:/usr/local/LAMP/mysql/bin
export PATH
# 最后命令行执行生效指令
source /etc/profile

test

# 在终端命令行下执行以下命令
echo $PATH
# 或
env

Guess you like

Origin blog.csdn.net/qq_34125713/article/details/128643525