How to modify environment variables in Linux system

1. Modify environment variables in Linux system

Under normal circumstances, there are two types of files that can be modified to change environment variables:
the first type is "system global environment variables" , which can be applied to all users included in the entire system after modification; (file: /etc/profile)

The second category is "local environment variables" , which is a unique environment variable for each user. After modification, it will only take effect for the current user, and each user is independent of each other; (file: /home/<username>/.bashrc)

1.1. Several common environment variables

PATH:包含可执行程序的绝对路径
HISTSIZE:保存历史命令的记录条数
SHELL:当前所使用的 shell 类型
HOME:指定用户的主工作目录

1.2. Common ways to modify environment variables

Generally, modifying environment variables is almost adding the running directory or environment variable values ​​of some programs. Here are a few commonly used usages of adding environment variables:

Add temporary environment variables

# 使用 export 命令可设置临时的环境变量,仅作用于当前 shell (即当前窗口)

export <环境变量名> = <路径>

Clear environment variables

unset <环境变量名>

insert image description here

To add permanent environment variables
modify /etc/.bashrcthe file or /etc/porfilefile:

# 一般在文件末尾处添加

# 追加可执行程序路径,系统查找时会在 $PATH 包含的路径中查找
export PATH=<路径>:$PATH

# 添加环境变量
export <环境变量名>=<路径>

After the modification is completed, save and exit, you need to execute sourcethe command to make it take effect:

# 若修改的文件是 /etc/profile,则执行
source /etc/profile

# 若修改的是其它文件,则对应 source 其它文件即可生效

2. Several problems that may be encountered when modifying environment variables

After modifying .bashrcthe file and executing sourcethe command to make it take effect, you may encounter the following errors.

2.1. command not found: shopt…command not found: complete…

Such a similar error is caused by not modifying the corresponding shellenvironment variable configuration file. The error message in Chinese is as follows:

┌──(imaginemiracle㉿Kali-IM)-[~]
└─$ source .bashrc
找不到命令 “shopt”,您的意思是:
  “shout” 命令来自 Debian 软件包 libshout-tools
尝试 sudo apt install <deb name>
找不到命令 “shopt”,您的意思是:
  “shout” 命令来自 Debian 软件包 libshout-tools
尝试 sudo apt install <deb name>
找不到命令 “shopt”,您的意思是:
  “shout” 命令来自 Debian 软件包 libshout-tools
尝试 sudo apt install <deb name>
找不到命令 “shopt”,您的意思是:
  “shout” 命令来自 Debian 软件包 libshout-tools
尝试 sudo apt install <deb name>
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
complete:未找到命令
/usr/share/bash-completion/bash_completion:1596: parse error near `|'
                                                                                                             
\[\e]0;\u@\h: \w\a\]\[\033[;32m\]┌──(\[\033[1;34m\]\u㉿\h\[\033[;32m\])-[\[\033[0;1m\]\w\[\033[;32m\]]\n\[\033[;32m\]└─\[\033[1;34m\]$\[\033[0m\] 

insert image description here

2.2. Solve the problem command not found: shoptof error reporting

The reason for this error mentioned above is that the correct corresponding configuration file was not modified. Let's see how to know the correct configuration file.

2.2.1. View the shell of the current system

View the current system allshell

cat /etc/shells


See what's in useshell

echo &SHELL

insert image description here

2.2.2. Modify the configuration file of the corresponding shell

Here you can see that the author's system uses zsh, so the corresponding configuration file should be.zshrc

insert image description here
Then modify .zshrcand execute sourcewithout any error message.

#over

Guess you like

Origin blog.csdn.net/qq_36393978/article/details/130683444