2015-05-05 54 views
2

這是的一部分我的〜/ .bashrc:bash提示符殼

prompt(){ 
    local EXIT="$?" # return code 
    PS1="" 
    local red="\[\033[0;31m\]" # text colour 
    local purple="\[\033[0;35m\]" # text colour 
    local normal="\[\033[0m\]" # text colour 

    if [ $EXIT == 0 ]; then # $EXIT colour based upon its value 
     local return="${normal}${?}" 
    else 
     local return="${red}${?}${normal}" 
    fi 

    PS1+="${normal}[${purple}\\D{%-l:%M%P}${normal}]${return} \\[\\e]0; \ 
    \\[email protected]\\h: \\w\\a\\]${debian_chroot:+($debian_chroot)}\\[email protected]\\h:\\w\\$ " 
} 

export PROMPT_COMMAND=prompt 

這是我在提示殼侏儒末端(正確顯示):

[5:01pm]0 [email protected]:~$ 

但是當我切換到TTY控制檯,我登錄後,該顯示:

[5:05pm]0 ;[email protected]: [email protected]:~$ 

這種情況與定期用戶,還可以在同一個操作系統上的同一個筆記本上使用root。 顏色始終正確顯示,唯一的問題是顯示提示的方式。


Sofware版本:

  • GNU的bash,版本4.3.11(1)-release(i686的-PC-Linux的GNU),
  • Ubuntu的Gnome的14.04。

的PS1值的一些解釋:

[5:01pm] # current time 
0   # return/exit code of the last command (0 can be any number; 
      # if return code has a non-zero value, it turns red) 

PS - 目前在TTY控制檯,我需要source ~/.bashrc能夠運用自己所有的設置。 (1)tty控制檯自己的.bashrc在哪裏? (2)或者如何設置它以使用~/.bashrc?迅速

作爲公認的答案表明,在PS1變量,有哪些應該被忽略的一部分,不同的行爲


解決方案因此,我只是改變了函數的最後一行

PS1+="${normal}[${purple}\\D{%-l:%M%P}${normal}]${return} \ 
${debian_chroot:+($debian_chroot)}\\[email protected]\\h:\\w\\$ " 

的郵政Scriptum的溶液(PS)

tty控制檯使用~/.bashrc_profile而不是~/.bashrc,因爲(如下面評論中的@chepner所述)後者來源於GUI終端仿真器(因爲它通常啓動非登錄交互式shell)。 tty console是一個交互式shell。

我的解決辦法是添加以下~/.bash_profile

. ~/.bashrc 
+1

'.bashrc'特定於登錄用戶,而不是終端類型。 – chepner

+0

所以我也想到了,但即使我使用相同的信任方式登錄到GUI mod(並因此到gnome終端)和tty控制檯,仍然在tty控制檯中,如果我想使用'〜/ .bashrc '設置,首先我需要輸入'source〜/ .bashrc'命令。 – tukusejssirs

+2

當您登錄到控制檯時,登錄程序正在啓動一個登錄shell,該登錄shell源自'.bash_profile',而不是'.bashrc'。一個GUI終端仿真器通常會啓動一個非登錄交互式shell,其源自'.bashrc',而不是'.bash_profile',因爲您的GUI大概是從登錄shell開始的。在'.bash_profile'中加入'source〜/ .bashrc'是一種非常普遍的做法,這樣'.bashrc'就可以用於所有交互式shell,無論它們是否是登錄shell。 – chepner

回答

2

你有\\[\\e]0; \\[email protected]\\h: \\w\\a\\]PS1\\[email protected]\\h:\\w\\$

問題是爲什麼你的終端沒有顯示第一組,但你的控制檯是。

我相信答案是,您已將第一個包含在\[...\]區塊中的第一組指示爲非打印並且不佔用空間(這就是爲什麼您需要在\[...\]中附上顏色代碼以避免提示短於所述終端期望當代碼不產生可見字符。

這導致侏儒末端丟棄從輸出一切(甚至可見字符)/所述\[...\]塊的內容。

的控制檯,大概是打印可見字符(並忽略非打印字符)(我不知道這是否會導致提示s或者不是。)

這裏的解決方案是刪除第一個(似乎是無意的)轉義集。