首先了解一個變量:
PS(Prompt Sign):是指命令提示符,例如ubuntu 12.04終端下的:zhangrui@thinkPad-Edge-E430c:~$
修改ubuntu的命令提示符主要是通過修改變量PS1和PS2的值
PS1是主要的提示符設置,在ubuntu一般爲:
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
PS2是次要提示符,一般爲“>”
在設定PS1環境變量時,我們需要用到預設的一些特殊變量,分類如下:
主要信息:
\u:當前登錄用戶名
\h:當前計算機名稱(比如上面的thinkPad-Edge-E430c)
\H:完整的主機名稱
\w:完整的工作目錄名稱。家目錄會以 ~ 取代
\W:利用 basename 取得工作目錄名稱,所以僅會列出最後一個目錄名
\$:提示字符,如果是root用戶,提示字符爲#,否則就是$
時間顯示:
\t:顯示時間(24小時制,如:HH:MM:SS)
\T:顯示時間(12小時制)
\@:顯示時間(AM/PM顯示)
\d:顯示日期,格式爲 Weekday Month Date,例如 "Mon Aug 1"
Shell信息:
\#:下達的第幾個指令
\v:Bash的版本信息
\V:Bash的發佈版本號
\S:Shell名稱
\!:Bash命令的歷史編號
\j:job序號
\l:Shell的終端名稱
控制符:
\\:\
\n:換行
\r
\]:]
\e:Esc
\[:[
舉例:
打開~/.bashrc文件,作如下修改:
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
# force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='\n\[\e[33m\]\u:>> \w\n\[\e[32m\]\h \[\e[36m\]\t \[\e[31m\]\$\[\e[0m\] '
else
PS1='\n\u:>> \w\n\h \t \$ '
fi
unset color_prompt force_color_prompt
保存之後,重新打開終端,效果如下:
如果將 # force_color_prompt=yes 註釋打開 force_color_prompt=yes
保存之後,重新打開終端,效果如下: