Linux設定SHEEL提示符的格式和顏色

neverinit發表於2017-09-10

本文的內容在Red Hat Enterprise Linux Server release 6.5 (Santiago)下測試透過。

1.瞭解SP1SP2引數

SP1bash中命令輸入的主提示符,如“[oracle@prod ~]$”。

SP2:當命令特別長的時候,換行輸入時的提示符,預設是“>”。

可以在終端中輸入命令檢視系統預設引數:

  1. [root@prod ~]# echo $PS1
  2. [\u@\h \W]\$
  3. [root@prod ~]# echo $PS2
  4. >

或者可以檢視set:

  1. [root@prod ~]# set | grep PS1
  2. PS1='[\u@\h \W]\$ '
  3. [root@prod ~]# set | grep PS2
  4. PS2='> '

如果需要檢視提示符引數中各部分的定義,可以執行man bash命令,找到PROMPTING部分說明:

  1. PROMPTING
  2.        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 customized by inserting a number of backslash-escaped special characters that are decoded as follows:
  3.               \a an ASCII bell character (07)
  4.               \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
  5.               \D{format}
  6.                      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.
  7.                      The braces are required
  8.               \e an ASCII escape character (033)
  9.               \h the hostname up to the first ‘.
  10.               \H the hostname
  11.               \j the number of jobs currently managed by the shell
  12.               \l the basename of the shell’s terminal device name
  13.               \n newline
  14.               \r carriage return
  15.               \s the name of the shell, the basename of $0 (the portion following the final slash)
  16.               \t the current time in 24-hour HH:MM:SS format
  17.               \T the current time in 12-hour HH:MM:SS format
  18.               \@ the current time in 12-hour am/pm format
  19.               \A the current time in 24-hour HH:MM format
  20.               \u the username of the current user
  21.               \v the version of bash (e.g., 2.00)
  22.               \V the release of bash, version + patch level (e.g., 2.00.0)
  23.               \w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
  24.               \W the basename of the current working directory, with $HOME abbreviated with a tilde
  25.               \! the history number of this command
  26.               \# the command number of this command
  27.               \$ if the effective UID is 0, a #, otherwise a $
  28.               \nnn the character corresponding to the octal number nnn
  29.               \\ a backslash
  30.               \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  31.               \] end a sequence of non-printing characters

  32.        The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).

簡單翻譯一下序列說明:

  1. \a 一個ASCII響鈴字元(07)
  2. \d "Weekday Month Date"格式的日期(比如"Tue May 26"
  3. \D{format} 透過向strftime傳值後回顯自定義日期格式
  4. \e ASCII跳脫字元(033)
  5. \h 主機名的第一部分,擷取到第一個'.'
  6. \H 主機的全稱
  7. \j 在當前shell中管理的程式數
  8. \l shell的終端裝置名稱的基名
  9. \n 換行符
  10. \r 回車符
  11. \s shell的名稱,當前指令碼的名稱,不包括路徑
  12. \t HH:MM:SS格式的24小時制時間
  13. \T HH:MM:SS格式的12小時制時間
  14. \@ am/pm格式的12小時制時間
  15. \A HH:MM格式的12小時制時間
  16. \u 當前使用者的使用者名稱
  17. \v bash的的版本(如2.00)
  18. \V bash版本,包括補丁級別(如2.00.0)
  19. \w 當前工作目錄,包括路徑
  20. \W 當前工作目錄的基名,不包括路徑
  21. \! 當前命令的歷史記錄編號
  22. \# 命令編號(只要您鍵入內容,它就會在每次提示時累加)
  23. \$ 如果您不是超級使用者 (root),則提示"$";如果您是超級使用者,則提示"#"
  24. \nnn 對應於八進位制數nnn的字元
  25. \\ 反斜槓
  26. \[ 開始一系列非列印字元,可以將終端控制序列嵌入到提示符中
  27. \] 結束一系列非列印字元


2.測試SP1SP2引數

這裡做一些關於SP1引數的測試(當然,使用者可以根據自己的喜好自行調整):

  1. [root@prod ~]# su - oracle
  2. [oracle@prod ~]$ set | grep PS1
  3. PS1='[\u@\h \W]\$ '
  4. [oracle@prod ~]$ export PS1='[\u@\h\d \W]\$ '
  5. [oracle@prodSun Sep 10 ~]$ export PS1='[\u@\h \v \W]\$ '
  6. [oracle@prod 4.1 ~]$ export PS1='[\u@\h \@ \W]\$ '
  7. [oracle@prod 07:07 AM ~]$ export PS1='[\u#\h \w]\$ '
  8. [oracle#prod ~]$ export PS1='[my oracle zone->\h \W]\$ '
  9. [my oracle zone->prod ~]$ cd /u01/app/oracle
  10. [my oracle zone->prod oracle]$ export PS1='[\u@\h \w]\$ '
  11. [oracle@prod /u01/app/oracle]$ export PS1='[\u@\h \W]\$ '
  12. [oracle@prod oracle]$ export PS1='[you can write some information here...\n\u@\h \W]\$ '
  13. [you can write some information here...
  14. oracle@prod oracle]$export PS1='[\u@\h \W]\$ '
  15. [oracle@prod oracle]$

當然,SP2引數也是可以做一些設定的:

  1. [oracle@prod oracle]$ set | grep PS2
  2. PS2='> '
  3. [oracle@prod oracle]$ export PS2='[\u@\h \W]> '
  4. [oracle@prod oracle]$ ls
  5. admin cfgtoollogs checkpoints diag fast_recovery_area oradata product
  6. [oracle@prod oracle]$ ll \
  7. [oracle@prod oracle]> fast_recovery_area/
  8. total 4
  9. drwxr-x---. 3 oracle oinstall 4096 Sep 9 09:46 PROD
  10. [oracle@prod oracle]$ export PS2='continue > '
  11. [oracle@prod oracle]$ ll \
  12. continue > fast_recovery_area/
  13. total 4
  14. drwxr-x---. 3 oracle oinstall 4096 Sep 9 09:46 PROD
  15. [oracle@prod oracle]$ export PS2='> '
  16. [oracle@prod oracle]$ ll \
  17. > fast_recovery_area
  18. total 4
  19. drwxr-x---. 3 oracle oinstall 4096 Sep 9 09:46 PROD
  20. [oracle@prod oracle]$


3.為提示符配置顏色

我們可以透過設定PS1PS2變數使提示符成為彩色,這樣就可以方便的找到頁面中輸入過的命令。

設定字元序列顏色的格式為:  \e[F;Bm

結束字元序列顏色的格式為:  \e[0m

如果需要為字元設定顏色,可以這樣設定:\e[F;Bm需要設定顏色的部分\e[0m

其中F為字型顏色,編號30~37B為背景色,編號40~47

顏色資訊表如下:

  1. F B
  2. 30 40 黑色
  3. 31 41 紅色
  4. 32 42 綠色
  5. 33 43 黃色
  6. 34 44 藍色
  7. 35 45 紫紅色
  8. 36 46 青藍色
  9. 37 47 白色

如果需要設定特殊顯示,如高亮顯示顯示,可以將字元序列顏色的格式\e[F;Bm中的F;B設定為1,即\e[1m,然後和顏色資訊加配置在一起,如\e[31;40m\e[1m

設定特殊顯示錶如下:  

  1. 0 OFF,關閉顏色
  2. 1 高亮顯示
  3. 4 顯示下劃線
  4. 5 閃爍顯示
  5. 7 反白顯示
  6. 8 顏色不可見

透過上述顏色資訊表和設定特殊顯示錶對顏色引數進行配置,即可為終端提示符設定個性化的顏色。

如現在的提示符為:PS1='[\u@\h \W]\$ '

現在要進行如下配置:

  1. 設定使用者名稱為紅色黑底,並且高亮顯示,即為:\e[31;40m\e[1m使用者名稱\e[0m
  2. 設定主機名為黃色白底,並且顯示下劃線,即為:\e[33;47m\e[4m主機名\e[0m
  3. 設定路徑為紫紅色黑底,即為\e[35;40m\e[0m

調整後的提示符為:


  1. PS1='[\e[31;40m\e[1m\u\e[0m@\e[33;47m\e[4m\h\e[0m \e[35;40m\W\e[0m]\$ '
效果如下圖所示:


當然,使用者可以自行對SP1SP2引數進行多次測試,完成顏色的調整。

注意:更多情況下可以透過XshellSecureCRT設定終端顯示顏色,如白底黑字,黑底白字,黑底綠字等,調整的過程可以和工具相結合,以獲取最舒適的顏色配置。

4.配置持久化儲存

透過在終端中執行export命令設定的變更只對當前終端有效,關閉之後再開啟另一個終端就會失效。如果需要將修改進行持久化儲存,需要將修改配置到系統配置檔案中。

export語句寫入/etc/profile/etc/bashrc對全部使用者生效,寫入~/.bash_profile~/.bashrc 只對當前使用者生效。

  1. [root@prod ~]# echo "export PS1='\e[33;40m[\u@\h \W]\$\e[0m '" >> /etc/bashrc
  2. [root@prod ~]# cat /etc/bashrc | grep PS1
  3. if [ "$PS1" ]; then
  4. [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  5. # if [ "$PS1" ]; then
  6. # PS1="[\u@\h:\l \W]\\$ "
  7. if [ "$PS1" ]; then
  8. export PS1='\e[33;40m[\u@\h \W]$\e[0m '
  9. [root@prod ~]# source /etc/bashrc

這樣,就可以保持修改的永久生效,而且,透過顏色的配置,還可以在整頁的命令中快速定位到上一條輸入的命令位置。



透過上述配置,即可實現在Linux系統中自定義設定SHEEL提示符的格式和顏色。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31394774/viewspace-2144749/,如需轉載,請註明出處,否則將追究法律責任。

相關文章