每次登录时 bash 提示符都会重置

每次登录时 bash 提示符都会重置

我问这个问题前段时间关于shell不显示路径的问题。最近我发现.bashrc没有获取来源(这应该是正常做法?)。目前,shell命令提示符是这样的:

-bash-4.3#

执行后source .bashrc,我可以获得预期的 shell 命令提示符:

root@ubuntu2011:~#

哪里ubuntu2011是机器名称。如何让我每次登录时的提示都是后一种?

ps.bashrc来自:cp /etc/skel/.bashrc ~/.bashrc


更新: 内容~/.profile如下:

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n

echo $BASH返回/bin/bash


更新: @terdon 问题的回答:

  1. 我如何登录服务器?

我用来登录ssh [email protected]root

  1. 这些命令是作为 root 执行的吗?

是的。


更新: 的输出ls -l ~/.{profile,bashrc,bash_profile,bash_login}

ls: cannot access /root/.bash_login: No such file or directory
-rw-r--r-- 1 root root   63 Dec 24  2012 /root/.bash_profile
-rw-r--r-- 1 root root 3637 May 17 17:00 /root/.bashrc
-rw-r--r-- 1 root root  140 Apr 23  2010 /root/.profile

答案1

发生这种情况是因为您通过 登录ssh。这归结为登录 shell 和非登录 shell 之间的差异。当您通过 ssh 连接时,您将运行登录交互式 shell。正如 中所解释的man bash,这种 shell 将:

当 bash 作为交互式登录 shell 或带有 --login 选项的非交互式 shell 被调用时,它首先从文件 /etc/profile 中读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的文件中读取并执行命令。启动 shell 时可以使用 --noprofile 选项来禁止此行为。

换句话说,~/.bashrc运行登录 shell 时默认会被忽略。简单的解决方案是从读取的文件之一显式获取它。正如您在上面所看到的,登录 shell 将首先尝试读取~/.bash_profile,如果不存在~/.bash_login,则它们会读取~/.profile。由于您有一个~/.bash_profile文件,因此您需要将这些行添加到其中:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

答案2

将其放入您的 ~/.profile 中

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

相关内容