现在的位置: 首页 > 综合 > 正文

Linux终端提示符PS1设置(颜色)

2013年09月28日 ⁄ 综合 ⁄ 共 2074字 ⁄ 字号 评论关闭

/d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
/H :完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux
/h :仅取主机的第一个名字,如上例,则为fc4,.linux则被省略
/t :显示时间为24小时格式,如:HH:MM:SS
/T :显示时间为12小时格式
/A :显示时间为24小时格式:HH:MM
/u :当前用户的账号名称
/v :BASH的版本信息
/w :完整的工作目录名称。家目录会以 ~代替
/W :利用basename取得工作目录名称,所以只会列出最后一个目录
/# :下达的第几个命令
/$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$

    CentOS中关于这些:
/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
/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

    于是可以根据这些指定自己喜欢的命令提示符格式:

PS1="/u@/h:/w/$ "

    但是这样设置之后显示的提示符是没有颜色的,如果希望加入颜色设置,可以这样:

PS1="/e[1;32m/u/e[m/e[1;33m@/e[m/e[1;35m/h/e[m:/w/$ "

   
此时,在终端中的显示应该是彩色的提示符了,其中的/e[1;xxm和/e[m就是改变终端字体颜色的部分,前者是修改颜色,后者是恢复到正常显示。但是
这时还会有问题,在提示符中输入第一行时,回车换行不正常,导致显示很混乱,因此,除了这个修改之外,还需要将全部非打印字符用专用的 bash
转义序列 "/[" 和 "/]" 括起来。这两个序列通知 bash,被括起来的字符不占用行上的任何空间
,这样就使自动换行能够继续正常工作。最终版本如下(可解决带颜色提示符的不换行问题):

PS1="/[
/e[1;32m/]
/u/[
/e[m/]
/[
/e[1;33m/]
@/[
/e[m/]/[
/e[1;35m/]
/h/[
/e[m/]
:/w/$ "

抱歉!评论已关闭.