问题描述
在.bashrc文件中配置环境变量 export PATH="$PATH:$HOME/Library/Python/3.8"
后,运行了命令source ~/.bashrc
执行pipenv命令有效,然后新开一个终端后,提示 command not found
原因
执行source ~/.bashrc
后生效,说明配置成功了,新打开终端未生效说明新打开终端时未加载 .bashrc
文件。
因为配置的环境是Mac,而Mac OS环境变量的默认配置文件为~/.zshrc
, 所以在~/.zshrc·
中添加source ~/.bashrc
即可
由于经常遇到此类环境变量配置问题,了解了一下login shell 和 non-login shell的区别,
login shell 和 non-login shell
login shell 读取/etc/profile, 然后依次读取~/.bash_profile, ~/.bash_login, and ~/.profile,如果已找到则后面的文件不会被读取
non-login shell 读取~/.bashrc来应用新的环境变量,比如在已打开的终端再输入bash,未输入密码,那么新开的bash就是non-login的(如果输入的bash --login 则还是login shell)
同理su user
,su - user
分别标识non-login shell和login shell,通过man su
查看的解释
.......
-, -l, --login
Start the shell as a login shell with an environment similar to a real login:
......
通过shopt -q login_shell && echo 'Login shell' || echo 'Non-login shell'
可以知道是login shell or non-login shell
interactive 和 non-interactive shell
交互式shell:在终端输入命令
非交互式shell: 执行shell scriptss
通过命令[[ $- == *i* ]] && echo 'Interactive shell' || echo 'Non-interactive shell'
可以知道是interactive还是non-interactive shell