Linux怎么設(shè)置SHEEL提示符的格式和顏色

本篇內(nèi)容主要講解“Linux怎么設(shè)置SHEEL提示符的格式和顏色”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Linux怎么設(shè)置SHEEL提示符的格式和顏色”吧!

成都創(chuàng)新互聯(lián)提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì),品牌網(wǎng)站建設(shè),1元廣告等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,十余年的網(wǎng)站開(kāi)發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破數(shù)千家,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.

1.了解SP1和SP2參數(shù)

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

SP2:當(dāng)命令特別長(zhǎng)的時(shí)候,換行輸入時(shí)的提示符,默認(rèn)是“>”。

可以在終端中輸入命令查看系統(tǒng)默認(rèn)參數(shù):

  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='> '

如果需要查看提示符參數(shù)中各部分的定義,可以執(zhí)行man bash命令,找到PROMPTING部分說(shuō)明:

  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).

簡(jiǎn)單翻譯一下序列說(shuō)明:

  1. \a 一個(gè)ASCII響鈴字符(07)

  2. \d "Weekday Month Date"格式的日期(比如"Tue May 26")

  3. \D{format} 通過(guò)向strftime傳值后回顯自定義日期格式

  4. \e ASCII轉(zhuǎn)義字符(033)

  5. \h 主機(jī)名的第一部分,截取到第一個(gè)'.'

  6. \H 主機(jī)的全稱(chēng)

  7. \j 在當(dāng)前shell中管理的進(jìn)程數(shù)

  8. \l shell的終端設(shè)備名稱(chēng)的基名

  9. \n 換行符

  10. \r 回車(chē)符

  11. \s shell的名稱(chēng),當(dāng)前腳本的名稱(chēng),不包括路徑

  12. \t HH:MM:SS格式的24小時(shí)制時(shí)間

  13. \T HH:MM:SS格式的12小時(shí)制時(shí)間

  14. \@ am/pm格式的12小時(shí)制時(shí)間

  15. \A HH:MM格式的12小時(shí)制時(shí)間

  16. \u 當(dāng)前用戶(hù)的用戶(hù)名

  17. \v bash的的版本(如2.00)

  18. \V bash版本,包括補(bǔ)丁級(jí)別(如2.00.0)

  19. \w 當(dāng)前工作目錄,包括路徑

  20. \W 當(dāng)前工作目錄的基名,不包括路徑

  21. \! 當(dāng)前命令的歷史記錄編號(hào)

  22. \# 命令編號(hào)(只要您鍵入內(nèi)容,它就會(huì)在每次提示時(shí)累加)

  23. \$ 如果您不是超級(jí)用戶(hù) (root),則提示"$";如果您是超級(jí)用戶(hù),則提示"#"

  24. \nnn 對(duì)應(yīng)于八進(jìn)制數(shù)nnn的字符

  25. \\ 反斜杠

  26. \[ 開(kāi)始一系列非打印字符,可以將終端控制序列嵌入到提示符中

  27. \] 結(jié)束一系列非打印字符

2.測(cè)試SP1和SP2參數(shù)

這里做一些關(guān)于SP1參數(shù)的測(cè)試(當(dāng)然,用戶(hù)可以根據(jù)自己的喜好自行調(diào)整):

  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]$

當(dāng)然,SP2參數(shù)也是可以做一些設(shè)置的:

  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.為提示符配置顏色

我們可以通過(guò)設(shè)置PS1和PS2變量使提示符成為彩色,這樣就可以方便的找到頁(yè)面中輸入過(guò)的命令。

設(shè)置字符序列顏色的格式為:  \e[F;Bm

結(jié)束字符序列顏色的格式為:  \e[0m

如果需要為字符設(shè)置顏色,可以這樣設(shè)置:\e[F;Bm需要設(shè)置顏色的部分\e[0m

其中F為字體顏色,編號(hào)30~37;B為背景色,編號(hào)40~47。

顏色信息表如下:

  1. F B

  2. 30 40 黑色

  3. 31 41 紅色

  4. 32 42 綠色

  5. 33 43 黃色

  6. 34 44 藍(lán)色

  7. 35 45 紫紅色

  8. 36 46 青藍(lán)色

  9. 37 47 白色

如果需要設(shè)置特殊顯示,如高亮顯示顯示,可以將字符序列顏色的格式中\e[F;Bm中的F;B設(shè)置為1,即\e[1m,然后和顏色信息加配置在一起,如\e[31;40m\e[1m。

設(shè)置特殊顯示表如下:  

  1. 0 OFF,關(guān)閉顏色

  2. 1 高亮顯示

  3. 4 顯示下劃線(xiàn)

  4. 5 閃爍顯示

  5. 7 反白顯示

  6. 8 顏色不可見(jiàn)

通過(guò)上述顏色信息表和設(shè)置特殊顯示表對(duì)顏色參數(shù)進(jìn)行配置,即可為終端提示符設(shè)置個(gè)性化的顏色。

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

現(xiàn)在要進(jìn)行如下配置:

  1. 設(shè)置用戶(hù)名為紅色黑底,并且高亮顯示,即為:\e[31;40m\e[1m用戶(hù)名\e[0m

  2. 設(shè)置主機(jī)名為黃色白底,并且顯示下劃線(xiàn),即為:\e[33;47m\e[4m主機(jī)名\e[0m

  3. 設(shè)置路徑為紫紅色黑底,即為:\e[35;40m路徑\e[0m

調(diào)整后的提示符為:

  1. PS1='[\e[31;40m\e[1m\u\e[0m@\e[33;47m\e[4m\h\e[0m\e[35;40m\W\e[0m]\$ '

效果如下圖所示:
Linux怎么設(shè)置SHEEL提示符的格式和顏色

當(dāng)然,用戶(hù)可以自行對(duì)SP1和SP2參數(shù)進(jìn)行多次測(cè)試,完成顏色的調(diào)整。

注意:更多情況下可以通過(guò)Xshell,SecureCRT設(shè)置終端顯示顏色,如白底黑字,黑底白字,黑底綠字等,調(diào)整的過(guò)程可以和工具相結(jié)合,以獲取最舒適的顏色配置。

4.配置持久化保存

通過(guò)在終端中執(zhí)行export命令設(shè)置的變更只對(duì)當(dāng)前終端有效,關(guān)閉之后再打開(kāi)另一個(gè)終端就會(huì)失效。如果需要將修改進(jìn)行持久化保存,需要將修改配置到系統(tǒng)配置文件中。

將export語(yǔ)句寫(xiě)入/etc/profile或/etc/bashrc對(duì)全部用戶(hù)生效,寫(xiě)入~/.bash_profile或~/.bashrc 只對(duì)當(dāng)前用戶(hù)生效。

  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

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

Linux怎么設(shè)置SHEEL提示符的格式和顏色

到此,相信大家對(duì)“Linux怎么設(shè)置SHEEL提示符的格式和顏色”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)頁(yè)名稱(chēng):Linux怎么設(shè)置SHEEL提示符的格式和顏色
文章分享:http://muchs.cn/article34/johepe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、建站公司、電子商務(wù)、企業(yè)建站域名注冊(cè)、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)