Linux的指令提示符的格式是由系統變量PS1和PS2來定義的。
PS1:就是使用者平時的提示符。 PS2:第一行沒輸完,等待第二行輸入的提示符。 設定格式如下所示: PS1=‘\h:\W \u\$’
其中\h \W之類的解釋,在fc8下面 man bash,顯示結果如下:
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the
secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be cus-
tomized by inserting a number of backslash-escaped special characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the prompt string; an empty
format results in a locale-specific time representation. The braces are required
\e an ASCII escape character (033)
\h the hostname up to the first ‘.’
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell’s terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal control
sequence into the prompt
\] end a sequence of non-printing characters
常用的幾個選項參照如下: /d :代表日期,格式為weekday month date,例如:"Mon Aug 1" /H :完整的主機名稱。例如:我的機器名稱為:fc8.RedHat,則這個名稱就是 fc8.RedHat /h :僅取主機的第一個名字,如上例,則為fc4,. RedHat則被省略 /t :顯示時間為24小時格式,如:HH:MM:SS /T :顯示時間為12小時格式 /A :顯示時間為24小時格式:HH:MM /u :目前使用者的賬号名稱 /v :BASH的版本資訊 /w :完整的工作目錄名稱。家目錄會以 ~代替 /W :利用basename取得工作目錄名稱,是以隻會列出最後一個目錄 /# :下達的第幾個指令 /$ :提示字元,假如是root時,提示符為:# ,普通使用者則為:$
-------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------
我們能夠通過配置PS1變量使提示符成為彩色。在PS1中配置字元序列顔色的格式為: /[/e[F;Bm/] 其中``F''為字型顔色,編号30~37;``B''為背景色,編号40~47。 可通過``/e[0m''關閉顔色輸出;特别的,當B為1時,将顯示加亮加粗的文字,周詳請看下面的顔色表和代碼表。 顔色表
前景 | 背景 | 顔色 |
---|---|---|
30 | 40 | 黑色 |
31 | 41 | 紅色 |
32 | 42 | 綠色 |
33 | 43 | 黃色 |
34 | 44 | 藍色 |
35 | 45 | 紫紅色 |
36 | 46 | 青藍色 |
37 | 47 | 白色 |
代碼 | 意義 |
---|---|
OFF | |
1 | 高亮顯示 |
4 | 下劃線 |
5 | 閃爍 |
7 | 反白顯示 |
8 | 不可見 |
一般普通配置是 PS1='[\u@\h \W]$'
顯示結果類似如下:
-bash-3.2$
-bash-3.2$
-bash-3.2$ PS1='[\u@\h \W]$'
[[email protected] ~]$
[[email protected] ~]$
要加上顔色的話,參考網上的一個比較經典的配置。 root賬戶登陸顯示紅色,普通賬戶登陸顯示藍色。 将以下代碼寫入/etc/profile.
GROUP=`id -gn`
if [ "$USER" = "root" ]; then
PS1='[\[\e[31;1m\]\u\e[0m@\h \W]\$'
else
PS1='[\[\e[34;1m\]\u\e[0m@\h \W]\$'
fi
export PS1
每次啟動shell的時候,都會導入上述配置。 如果要在目前shell起作用,那麼需要輸入 source /etc/profile