每次登入時 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

相關內容