修改终端提示符

Linux终端提示符,是通过环境变量PS1进行设置的。

以ubuntu为例,可以通过man bash来查看bash手册,找到PS1的说明,以下为摘取的部分内容:

PROMPTING
       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:
              \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 (uses the value of the PROMPT_DIRTRIM
                     variable)
              \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,例如:"Tue May 26"

\H:#完整的主机名称。

\h:#仅取主机的第一个"."之前名字

\t:#显示时间为24小时格式,如:HH:MM:SS

\T:#显示时间为12小时格式

\A:#显示时间为24小时格式:HH:MM

\u:#当前用户的账号名称

\v:#BASH的版本信息

\w:#完整的工作目录名称。家目录会以 ~代替

\W:#利用basename取得工作目录名称,所以只会列出最后一个目录

\#:#下达的第几个命令

\$:#提示字符,如果是root时,提示符为:# ,普通用户则为:$

修改提示符,可以通过修改.bashrc实现。

打开.bashrc,可以看到有关PS1的一些设置。


在终端输出PS1的值看最后的设置内容。



1、修改终端提示符内容

当当前路径名太长时,往往后面再写命令就会被分成2行,既不美观,也难以看清具体命令。有2个方法可以处理

1) 将完整的工作目录名换成当前目录名,即将\w改成\W

这样处理会简洁一些,但无法直接看出当前目录的绝对路径

2) 将提示符放在一行,自己要输入的内容另起一行

这样处理简单明了,一眼看出当前信息,以及区分了提示信息和自己要输入的内容。缺点就是会占据2行。

根据我自己个人喜好,我偏向于第2种方法,喜欢设置的格式如下:

PS1="[\u@\h] \w\n\$ "
显示为:

[用户名@主机] 完整目录
提示符 


2、修改终端提示符颜色

一般没经过设置的终端就2种颜色:统一底色,统一字体颜色。

像我这类好色之人,对着那么单调的颜色,会觉得乏味,还是自己手动来改改!

在PS1中设置字符序列颜色的格式为:[\e[E;F;Bm]

其中,'E'为效果控制代码:

  代码 意义

  -------------------------

  0 OFF

  1 高亮显示

  4 underline

  5 闪烁

  7 反白显示

  8 不可见

'F'为字体颜色,编号30~37;'B'为背景色,编号40~47。下面看下颜色表:

  前景背景 颜色

  ------------------------

  30 40 黑色

  31 41 红色

  32 42 绿色

  33 43 黄色

  34 44 蓝色

  35 45 紫红色

  36 46 青蓝色

  37 47 白色

以下是我的设置内容:


效果如下:


这样看起来舒服多了。


最后,总结一下所有的设置吧

export PS1="["
PS1="$PS1\[\e[1;31m\]\u"
PS1="$PS1\[\e[m\]@"
PS1="$PS1\[\e[1;33m\]\h"
PS1="$PS1\[\e[m\]] "
PS1="$PS1\[\e[1;36m\]\w"
PS1="$PS1\[\e[m\]\n\$ "

分享快乐!快乐分享!

PS: 用CSDN插入代码后显示错乱了,还是以上面的截图为准去设置吧

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值