溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Linux怎么設置SHEEL提示符的格式和顏色

發布時間:2021-08-18 13:58:53 來源:億速云 閱讀:173 作者:chen 欄目:建站服務器

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


1.了解SP1和SP2參數

SP1:bash中命令輸入的主提示符,如“[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.測試SP1和SP2參數

這里做一些關于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.為提示符配置顏色

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

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

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

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

其中F為字體顏色,編號30~37;B為背景色,編號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]\$ '

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

當然,用戶可以自行對SP1和SP2參數進行多次測試,完成顏色的調整。

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

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提示符的格式和顏色

到此,相信大家對“Linux怎么設置SHEEL提示符的格式和顏色”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女