Linux设置SHEEL提示符的格式和颜色

1.了解SP1SP2参数

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

猜你喜欢

转载自www.linuxidc.com/Linux/2017-10/147438.htm
今日推荐