相信很多人已经看厌了Linux已成不变的命令提示符的颜色,多数人要么使用默认的绿色,要么在使用PUTTY的时候设置成绿色的,不知道是否有人想到提示符可以设置成其他的颜色呢,本文就说明命令提示符变量PS1的设置。

1、PS1变量简介

PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置。

可以使用 man bash命令查看bash手册,找到该变量支持的特殊字符,以及这些特殊字符的意义:

 
  1. \a     #an ASCII bell character (07)
  2. \d     #the date in "Weekday Month Date" format (e.g.,  "Tue  May
  3. 26")
  4. \D{format}
  5. #the  format  is  passed  to strftime(3) and the result is
  6. #inserted into the prompt string; an empty format  results
  7. #in a locale-specific time representation.  The braces are
  8. #required
  9. \e     #an ASCII escape character (033)
  10. \h     #the hostname up to the first ‘.’
  11. \H     #the hostname
  12. \j     #the number of jobs currently managed by the shell
  13. \l     #the basename of the shell’s terminal device name
  14. \n     #newline
  15. \r     #carriage return
  16. \s     #the name of the shell, the basename of  $0  (the  portion
  17. #following the final slash)
  18. \t     #the current time in 24-hour HH:MM:SS format
  19. \T     #the current time in 12-hour HH:MM:SS format
  20. \@     #the current time in 12-hour am/pm format
  21. \A     #the current time in 24-hour HH:MM format
  22. \u     #the username of the current user
  23. \v     #the version of bash (e.g., 2.00)
  24. \V     #the release of bash, version + patch level (e.g., 2.00.0)
  25. \w     #the current working  directory,  with  $HOME  abbreviated
  26. #with  a tilde (uses the value of the PROMPT_DIRTRIM vari-
  27. #able)
  28. \W     #the basename of the current working directory, with $HOME
  29. #abbreviated with a tilde
  30. \!     #the history number of this command
  31. \#     #the command number of this command
  32. \$     #if the effective UID is 0, a #, otherwise a $
  33. \nnn   #the character corresponding to the octal number nnn
  34. \\     #a backslash
  35. \[     #begin  a sequence of non-printing characters, which could
  36. #be used to embed a terminal  control  sequence  into  the
  37. #prompt
  38. \]     #end a sequence of non-printing characters

下面我把常用的特殊字符做中文解释:

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

2、颜色设置说明

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

其中“F”为字体颜色,编号30~37;“B”为背景色,编号40~47。

下面看下颜色表:

 
  1. 前景     背景      颜色
  2. ------------------------
  3.   30      40      黑色
  4.   31      41      红色
  5.   32      42      绿色
  6.   33      43      黄色
  7.   34      44      蓝色
  8.   35      45      紫红色
  9.   36      46      青蓝色
  10.   37      47      白色

效果控制代码:

 
  1. 代码              意义
  2. -------------------------
  3. 0                 OFF
  4. 1                 高亮显示
  5. 4                 underline
  6. 5                 闪烁
  7. 7                 反白显示
  8. 8                 不可见

3、示例

我们用下面这个例子,解析PS1变量来设置有颜色的命令提示符:

PS1="\[\e[32m\]#\##\[\e[1;31m\]\u@\[\e[36m\]\h \w]\$\[\e[m\"

说明:

‘\[\e[32m\]’用来设置‘#\##’的颜色为绿色,#\##就是显示现在运行的是第几条命令

‘[\e[31m\]’设置‘\u@’的颜色为红色并高亮显示,如果指定多个数字需要用分号隔开。\u@ 就是当前登录的用户名后跟一个‘@’符号。

‘\[\e[36m\]’设置‘\h\w’为青蓝色,\h表示主机名的第一位,如果主机名为centos6.lampbo.org,那么就显示centos6;\w将显示完整的绝对路径。

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

‘\[\e[m\]’使用来关闭颜色设置的。要是你没有这个的话;那么,你的命令提示符,包括你通过命令提示符输出的东西都是和最后一次的颜色设置相同。

为了能够在启动和登录是可以保持刚刚设置的变量,需要将PS1的设置加入到用户home目录的.bashrc文件后。

额外的示例:

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

(2):编辑.bashrc,加入以下内容:

 
  1. c_1="\[\e[0m%\]"
  2. c0="\[\e[30m%\]"
  3. c1="\[\e[31m%\]"
  4. c2="\[\e[32m%\]"
  5. c3="\[\e[33m%\]"
  6. c4="\[\e[34m%\]"
  7. c5="\[\e[35m%\]"
  8. c6="\[\e[36m%\]"
  9. c7="\[\e[37m%\]"
  10. PS1="$c0***** $c1\w $c2*** $c3<\u@\h> $c4***** $c5\! $c6***** $c7\t $c1***\n$c2\$ $c_1"; export PS1

Linux 修改终端命令提示符颜色的更多相关文章

  1. Linux修改Shell命令提示符及颜色

    Linux修改Shell命令提示符及颜色 1. Linux登录过程中加载配置文件顺序: /etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~ ...

  2. Linux:修改Shell命令提示符及颜色

    Linux修改Shell命令提示符及颜色   1. Linux登录过程中加载配置文件顺序: /etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ...

  3. linux 修改终端颜色

    要想修改终端的颜色,可以使用修改PS1环境变量的方式,也可以通过命令setterm来进行设置. 一.PS1 转自:修改linux终端命令行颜色 1.PS1 要修改linux终端命令行颜色,我们需要用到 ...

  4. [转载]Linux下终端字体颜色设置方法

    原文地址:Linux下终端字体颜色设置方法作者:router 网上类似的文章有很多,但是都是转来转去的,没有经过测试,按照很多文章的方法会造成你设置之后的终端在换行和删除输入字符时终端显示会乱七八糟, ...

  5. linux系统终端命令提示符设置(PS1)记录

    PS(Prompt Sign)指的是命令提示符,在日常运维工作中为了方面操作管理,有时会设定PS1环境变量.废话不多说,下面开始记录下Linux中PS1设置 在/etc/.bashrc中,找到PS1变 ...

  6. Linux修改终端显示前缀及环境变量

    Linux终端前面默认显示一长串,如: [work@aaa.baidu.com dir]$ 这是由PS1环境变量决定的: [work@aaa.baidu.com dir]$ echo $PS1 [\u ...

  7. Linux修改终端提示符

    打开~/.bashrc可以看到命令提示的内容为:\u@\h\w\$ \u表示用户名,\h表示主机名,\w表示当前目录,\$表示命令提示符(普通用户$,超级用户#) 这个命令提示符有点长,很碍事,\u@ ...

  8. Linux修改BASH命令提示符

    Shell命令提示符及颜色是由PS1来配置: 1.其中PS1常用的参数含义如下: \d :#代表日期,格式为weekday month date,例如:"Mon Aug 1" \H ...

  9. Linux 修改终端显示bash-1.4$

    先取得root权限,然后在终端如下操作[root@host]$su -然后输入密码接着[root@host]#PS1='[\u@\H \W]\$' 你取得root权限后在,在终端命令下输入这个,一定要 ...

随机推荐

  1. TIOJ1208 第K大连续和

    第k大的题一般都有点麻烦 pbds库的tree,需要研究一下https://codeforces.com/blog/entry/11080find_by_order() and order_of_ke ...

  2. 下拉列表事件 Dropdown iview

    <Dropdown @on-click="export"> <Button icon='md-log-out'> 000l <Icon type=&q ...

  3. docker 容器的网络

    容器的网络模式 bridge -net=bridge 默认网络.docker启动后创建一个docker0网桥,默认创建的容器也添加到这个网桥 [root@localhost ~]# ip a 1: l ...

  4. LinkdList和ArrayList异同、实现自定义栈

    //.LinkdList和ArrayList异同 //ArrayList以连续的空间进行存储数据 //LinkedList以链表的结构存储数据 //栈 先进后出 最上面是栈顶元素 arrayLiat自 ...

  5. MySQL系列(二)--MySQL存储引擎

    影响数据库性能的因素: 1.硬件环境:CPU.内存.存盘IO.网卡流量等 2.存储引擎的选择 3.数据库参数配置(影响最大) 4.数据库结构设计和SQL语句 MySQL采用插件式存储引擎,可以自行选择 ...

  6. 中位数II

    该题目与思路分析来自九章算法的文章,仅仅是自己做个笔记! 题目:数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数. 解答: 这道题是用堆解决的问题.用两个堆,max he ...

  7. Spoj8093 Sevenk Love Oimaster

    题目描述 题解: 对于所有n串建广义后缀自动机. (广义后缀自动机唯一区别就是每次将las附成1,并不需要在插入时特判) 建完后再建出parent树,然后用dfs序+树状数组搞区间不同种类. 其实就是 ...

  8. PLSQL Developer连接数据库报错ora-12514解决

    连接本地数据库报以上错误,需要修改本地数据库的监听配置文件(如下所示:) 比如我的本地oracle数据库相关文件在E:\app\user\product\11.2.0\dbhome_1下 然后在E:\ ...

  9. 关于linux安装kettle的总结

    一.部署准备 1.1 JDK安装配置 命令行键入“cd /etc”进入etc目录 命令行键入“vi profile”打开profile文件 敲击键盘ctrl+F到文件末尾 在末尾处,即第一个~的地方, ...

  10. UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)

    题意: 分析: 欧拉通路:图连通:图中只有0个或2个度为奇数的结点 这题我们只需要判断选择的边构成多少个联通块, 再记录全部联通块一共有多少个奇度顶点. 然后我们在联通块中连线, 每次连接两个联通块就 ...