Linux开机不能自动加载 ~/.bash_profile 文件的解决方法

终端启动分为login 和 non-login两种方式, non-login 方式启动是不加载~/.bash_profile 文件的。在登录情况下,一般shell会先读取~/.profile,再读取~/.bashrc。如果是在非登录情况下,shell只会去读取~/.bashrc。

1.Debian默认的shell是Bash
1.1 命令行 和 ssh 登录 ,首先读入 /etc/profile,这是对所有用户都有效的配置;然后依次寻找下面三个文件,这是针对当前用户的配置。
  ~/.bash_profile
  ~/.bash_login
  ~/.profile
需要注意的是,这三个文件只要有一个存在,就不再读入后面的文件了。比如,要是 ~/.bash_profile 存在,就不会再读入后面两个文件了。
1.2 图形界面登录:只加载 /etc/prfile 和 ~/.profile。也就是说,~/.bash_profile 不管有没有,都不会运行。

2.用户进入操作系统图形界面以后,常常会再手动开启一个shell。这个shell就叫做 non-login shell,意思是它不同于登录时出现的那个shell,不读取/etc/profile和.profile等配置文件。

3.终端模拟器通常会有选项来指定是开 login shell 还是 non-login shell,比如 xfce4-terminal 的。

下面给出两种解决方式:

1、把环境变量写入到~/.bashrc中,或者~/.profile。

2、在~/.profile里添加下面几行代码,即加载.profile文件同时调用~/.bash_profile文件。

if [ -n "$BASH_VERSION" ]; then
    if [ -f "$HOME/.bash_profile" ]; then
          "$HOME/.bash_profile"
    fi
fi

3、改变登录方式,注销系统并重新登录

# 编辑文件
vi ~/.config/deepin/deepin-terminal/config.conf
# 找到第56行,讲 false 修改为 true
run_as_login_shell=true
# :wq 保存退出
:wq

总结:
1. 图形界面登录是 non-login 不运行 .bash_profile 。
2. shell 和 ssh 登录打开 login shell ,会运行 .bash_profile。
3. 图形界面登录,可以指定 终端模拟器 为 non-login 还是 login ,但这只是指定 终端模拟器 ,经实测,和写到 .bashrc 效果一样,每次打开终端都会执行一次。
4. 要只在图形界面登录时执行一次,应该写入 .profile,而不是 .bash_profile ,或者在 .profile 增加一条 调用 .bash_profile。

猜你喜欢

转载自blog.csdn.net/carina_cao/article/details/78866697