我前段时间问过这个问题,关于 shell 不显示路径。最近我发现.bashrc没有获取来源(这应该是正常做法?)。目前,shell命令提示符是这样的:
-bash-4.3#
Run Code Online (Sandbox Code Playgroud)
执行后source .bashrc,我可以获得预期的 shell 命令提示符:
root@ubuntu2011:~#
Run Code Online (Sandbox Code Playgroud)
哪里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
Run Code Online (Sandbox Code Playgroud)
并echo $BASH返回/bin/bash
更新:@terdon 问题的答案:
我
ssh root@111.222.333.444用来登录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
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为您通过 登录ssh。这归结为登录 shell 和非登录 shell 之间的差异。当您通过 ssh 连接时,您将运行登录交互式 shell。正如 中所解释的man bash,这种 shell 将:
\n\n\n当 bash 作为交互式登录 shell 或作为带有 --login 选项的非 inter\xe2\x80\x90\n 活动 shell 调用时,它首先读取并执行来自文件 /etc/profile(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的文件中读取并执行命令。启动 shell 时可以使用 --noprofile 选项来禁止此行为。
\n
换句话说,~/.bashrc运行登录 shell 时默认会被忽略。简单的解决方案是从读取的文件之一显式获取它。正如您在上面所看到的,登录 shell 将首先尝试读取~/.bash_profile,如果不存在~/.bash_login,则它们会读取~/.profile。由于您有一个~/.bash_profile文件,因此您需要将这些行添加到其中:
if [ -f ~/.bashrc ]; then\n . ~/.bashrc\nfi\nRun Code Online (Sandbox Code Playgroud)\n