《学渣Linux笔记》——更改ls命令的输出颜色和命令提示符颜色(二)

II.更改命令提示符颜色

命令提示符的显示格式是由变量PS1决定的,首先我们查找GNU官方手册,发现如下内容(不是我喜欢写一大段,只是希望能养成查阅官方手册的习惯):

The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.

In addition, the following table describes the special characters which can appear in the prompt variables PS0, PS1, PS2, and PS4:

\a
A bell character. \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 escape character. \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
A newline. \r
A carriage return. \s
The name of the shell, the basename of $0 (the portion following the final slash). \t
The time, in 24-hour HH:MM:SS format. \T
The time, in 12-hour HH:MM:SS format. \@
The time, in 12-hour am/pm format. \A
The 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 + patchlevel (e.g., 2.00.0) \w
The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable). \W
The basename of $PWD, with $HOME abbreviated with a tilde. \!
The history number of this command. \#
The command number of this command. \$
If the effective uid is 0, #, otherwise $. \nnn
The character whose ASCII code is the octal value nnn. \\
A backslash. \[
Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt. \]
End a sequence of non-printing characters.

我们好像找不到我们想要的颜色选项,怎么办呢?我记得别人的是可以改颜色的呀!这时我想到了家目录下的.bashrc文件。让我打开看看:

$cat ~/.bashrc

然后我找到了这一段:

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

这一段里面好像只有[\033[01;32m]这段看不懂,再结合上一篇的内容(https://www.cnblogs.com/ZEROPONG/p/11809789.html),01、34这些是不是可能就是颜色代码呢?还真是!经过试验,我得到命令提示符的颜色选项格式如下:

开头:\[\033[F;B;Cm\]
结尾:\[\033[00m\]

注:为什么多了个C?结合上一篇,我们可以知道颜色设定有三个参数可以用此处亦是如此。此外,一旦出现结束符,后面的颜色选项将不会生效,类似于字符串输出时遇到'\0'。

我现在的PS1变量是什么样的呢?如下:

\033[32;40;1m\u@\h:\e[33;40m\W\033[0m$

有点不一样对吧?仔细看看上面附带的那一大段,里面有两行:

\[
Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.
\]
End a sequence of non-printing characters.

他们是不会打印出来的,于是经过试验,发现省去也可以(但是003与颜色代码之间的“[”不可以省,注意一下就可以发现,它少了个反斜杠,并不是非打印字符)。

另:以上方法简单直接,如不嫌麻烦,可在.bashrc文件中找到color_prompt变量,将变量值改为yes(该变量的作用具体见.bashrc内相关行),然后更改相应语句中的PS1变量。

《学渣Linux笔记》——更改ls命令的输出颜色和命令提示符颜色(二)的更多相关文章

  1. 转--配置linux下dircolors,ls命令和mac下iTerm2的颜色,让终端(iTerm2或Terminal)也色彩缤纷

    http://blog.bbkanba.com/%E9%85%8D%E7%BD%AElinux%E4%B8%8Bdircolorsls%E5%91%BD%E4%BB%A4%E5%92%8Cmac%E4 ...

  2. linux系统中ls命令的用法

    普通文件: -,f目录文件: d链接文件(符号链接): L设备文件:字符设备:c块设备:b命名管道: p套接字文件: s linux文件时间戳 时间分为三种类型:创建时间,修改时间:open访问时间: ...

  3. Python学习之旅:使用Python实现Linux中的ls命令

    一.写在前面 前几天在微信上看到这样一篇文章,链接为:https://mp.weixin.qq.com/s/rl6Sgv3uk_IpoFAx6cWa8w,在这篇文章中,有这样一段话,吸引了我的注意: ...

  4. 《学渣Linux笔记》——关于.bashrc与profile(涉及交互式与非交互式、登录与非登录shell)

    <学渣Linux笔记>--关于.bashrc与profile(涉及交互式与非交互式.登录与非登录shell) 1.基本概念(个人理解) 交互式shell:等待用户输入,并执行相应操作的sh ...

  5. 《The Linux Command Line》 读书笔记03 ls命令与长格式输出解释 文件权限

    ls命令与长格式输出解释 文件权限 ls命令 ls 命令用于列出目录内容,不带参数时列出当前工作目录的内容,也可以指定目标目录(可以指定多个),列出目标目录下的内容. ls命令的参数 ls -l 长格 ...

  6. linux下的ls命令

    在LINUX系统中有一个重要的概念:一切都是文件.其实这是UNIX哲学的一个体现,而Linux是重写UNIX而来,所以这个概念也就传承了下来.在UNIX系统中,把一切资源都看作是文件,包括硬件设备.U ...

  7. linux 中更改权限命令chown,chmod,chgrp

    写在前面,关于chown,chmod的区别 chown用法 用来更改某个目录或文件的用户名和用户组的 chown 用户名:组名 文件路径(可以是就对路径也可以是相对路径) 例1:chown root: ...

  8. Linux笔记2-常用命令

    1.简单的命令 cd /    切到根路径 cd ..    回到上一级目录 pwd    显示当前路径 touch newFile    创建文件 mkdir xx    创建目录 mv file1 ...

  9. Linux下执行ls命令提示CMake Error错误

    一.系统环境 Fedora10 二.出错情况 执行ls命令出现如下错误提示: CMake Error: The source directory "/etc/--color=auto&quo ...

随机推荐

  1. JS稀奇古怪题目

    JS稀奇古怪题目: 1.操作对象属性优先级高于普通赋值操作 var a = { n: 1 }; var b = a; //操作对象属性优先级高于普通赋值操作 a.x = a = { n: 2 }; c ...

  2. bzoj4066: 简单题 K-Dtree

    bzoj4066: 简单题 链接 bzoj 思路 强制在线.k-dtree. 卡常啊.空间开1e6就T了. 代码 #include <bits/stdc++.h> #define my_m ...

  3. 格式化输出(%用法和fomat用法)

    一:%用法 1.整数输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 2.浮点数输出 %f ——保留小数点后面六位有效数字 %.3f,保留3位小数位%e ——保留 ...

  4. 小数据池 is和== 再谈编码

    昨日回顾 上节课内容回顾 1. 字典 {key:value, key:value.....} 成对的保存数据 字典没有索引. 不能切片, 字典的key必须是可哈希的.不可变的 1. 增加: dic[新 ...

  5. mac 下使用Charles抓包华为手机app

    安装Charles:https://www.cnblogs.com/sea-stream/p/11577418.html 需要保证手机与电脑连接同一个Wi-Fi设置mac charles,打开代理 2 ...

  6. 作业:IEEE754浮点数

    人工转换: 5.75转换成二进制:101.11右移2位,补0:1.0111000000000000000000000000000000000000000000000000.10000000001 16 ...

  7. FZU Monthly-201906 tutorial

    FZU Monthly-201906 tutorial 题目(难度递增) easy easy-medium medium medium-hard hard 思维难度 AE B DG CF H A. X ...

  8. [技术博客]JSCover+selenium获得js代码覆盖率

    本文档讲解了我们是如何使用JSCover来获得Selenium的测试样例的js代码文件的执行覆盖率的. 事实上网上有挺多博客讲这玩意儿了,不过完全按照网上已有的教程去弄的的话,并无法满足我们的需要. ...

  9. Django实现自动发布(2视图-任务接收)

    上一篇服务版本的新增,是通过触发 gitlab 任务来实现的,那么如何得到任务的最终状态呢? 好在 gitlab 为我们提供了webhook,也就是消息钩子,可以发送pipeline消息到我们指定的地 ...

  10. FFmpeg 被声明为已否决 deprecated(2018 精)

    不用再取消SDL检查,不用再添加#pragma warning(disable :4996),下面才是正确的解决方法!! 以下是一些常见的deprecated问题,遇到下述没有列出的问题,可以打开相应 ...