「Linux」- 颜色化输出

  CREATED BY JENKINSBOT

问题描述

在终端中,我们经常查看日志输出(不管是自己的 Shell 脚本,还是运行其他程序),以了解程序执行情况。

但是,所有的终端日志颜色都是相同的(通常默认都是白色字体),

该笔记将记录:将终端输出变成彩色的方法

解决方案

方法一、ANSI escape code – 最古老的方法

使用 ANSI escape code 是最古老的方法:

# echo -e "\033[31;1;4mHello\033[0m"
Hello

该命令将输出带下划线的红色 Hello 字符串

方法二、grc

# grc tail -f /var/log/syslog

该命令将高亮 syslog 日志输出,通过配置文件可以进行更多控制

其他工具

colortail
GitHub – joakim666/colortail: Like the tail command line utility but with colors

colorex
GitHub – Scopart/colorex: A python script to display log files with colors.

colout
GitHub – nojhan/colout: Color text streams with a polished command line interface

TxtStyle
GitHub – armandino/TxtStyle: Command-line tool for colorizing console output and log files based on regular expressions

rainbow
GitHub – nicoulaj/rainbow: Colorize commands output or STDIN using patterns.

highlight
André Simon – Startseite

lolcat
GitHub – busyloop/lolcat: Rainbows and unicorns!

参考文献

terminal – Output in color – Bash – Unix & Linux Stack Exchange
GitHub – garabik/grc: generic colouriser