This is a portion of my ~/.bashrc:
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; \
\\u@\\h: \\w\\a\\]${debian_chroot:+($debian_chroot)}\\u@\\h:\\w\\$ "
}
export PROMPT_COMMAND=prompt
这是我在gnome-terminal中的提示符Shell(正确显示):
[5:01pm]0 user@host:~$
但是当我切换到tty控制台后,登录后显示如下内容:
[5:05pm]0 ;user@host: ~user@host:~$
这种情况发生在普通用户和root用户都在同一台笔记本电脑上,使用相同的操作系统。颜色始终显示正确,唯一的问题是提示符的显示方式。
软件版本:
- 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)}\\u@\\h:\\w\\$ "
Post Scriptum (PS)的解决方案
tty控制台使用~/.bashrc_profile
而不是~/.bashrc
,因为(如下面的@chepner评论所说),后者由GUI终端仿真器调用(因为它通常启动非登录交互式shell)。tty控制台是一个交互式shell。
我的解决方案是将以下内容添加到~/.bash_profile
中:
. ~/.bashrc
.bashrc
是针对登录用户而非终端类型的特定文件。 - chepner~/.bashrc
的设置,则首先需要输入source ~/.bashrc
命令。 - tukusejssirslogin
程序会启动一个登录shell,该shell源自.bash_profile
而不是.bashrc
。GUI终端仿真器通常会启动一个非登录交互式shell,该shell源自.bashrc
而不是.bash_profile
,因为您的GUI可能是从登录shell启动的。将source ~/.bashrc
包含在您的.bash_profile
中是一种相当普遍的做法,以便为所有交互式shell提供.bashrc
,无论它们是否是登录shell。 - chepner