How to change the output color of echo in Linux

大纲

1、The Bash Shell

2、Output in Color

3、How can do it?

1、The Bash Shell

Bash is the primary shell of the Linux machine, included here are some tips/tricks with the shell. Use the manual page and learn about PS1, PS2 and PROMPT_COMMAND. The tricks in here can be use in ~/.bash_profile or ~/.bashrc.

2、Output in Color

Cool terminal output involves color, here is how to set and reset output colors using terminal control characters. To print in color one must first send the control sequences for the color to the terminal, then output the message and reset the terminal, to be nice. One can also create nifty looking prompts with the teniques described here.

Console Color Table

The below key describes all the different sequences that can be sent to change the display format. The disclaimer should be made that "this is how the test computer performed".

Style           Foreground      Background
1st Digit       2nd Digit       3rd Digit
0 - Reset       30 - Black      40 - Black
1 - FG Bright   31 - Red        41 - Red
2 - Unknown     32 - Green      42 - Green
3 - Unknown     33 - Yellow     43 - Yellow
4 - Underline   34 - Blue       44 - Blue
5 - BG Bright   35 - Magenta    45 - Magenta
6 - Unknown     36 - Cyan       46 - Cyan
7 - Reverse     37 - White      47 - White

To instruct echo to interpret these codes you must tell it -en. The -e switch tells echo to interpret your escape sequences and -n tells echo not to make a newline at the end. The activation sequence is \033 or \e this starts the output modification codes. Next is a [ followed by a digits from the above list to represent the style, foreground and background. The sequence is terminated by the letter m. The sequence to get plain red on black would look like this: echo -en "\033[0;31;40m" or once could say echo -en "\033[0;31m" to only affect the foreground. Portions of the sequence can be left out but the digits are still interpreted in the same order. One can switch only the background by saying echo -en "\033[7;43m", this would change the background to yellow without affecting the current foreground settings. Once output is complete the terminal should be reset with echo -e "\033[0m".

Echo in Color Test Script

The code below is a sample of echo in color, it will run through all the sequences and ouput some nice looking tables. This can be use to test what different colors will look like as well as show the hidious combinations.

#/bin/bash
# Show all the colors of the rainbow, should be run under bash
for STYLE in 0 1 2 3 4 5 6 7; do
  for FG in 30 31 32 33 34 35 36 37; do
    for BG in 40 41 42 43 44 45 46 47; do
      CTRL="\033[${STYLE};${FG};${BG}m"
      echo -en "${CTRL}"
      echo -n "${STYLE};${FG};${BG}"
      echo -en "\033[0m"
    done
    echo
  done
  echo
done
# Reset
echo -e "\033[0m"

3、How can do it?

You can use these codes:

Black        0;30     Dark Gray     1;30
Blue         0;34     Light Blue    1;34
Green        0;32     Light Green   1;32
Cyan         0;36     Light Cyan    1;36
Red          0;31     Light Red     1;31
Purple       0;35     Light Purple  1;35
Brown/Orange 0;33     Yellow        1;33
Light Gray   0;37     White         1;37

And then use them like this in your script:

red=‘\e[0;31m‘
NC=‘\e[0m‘ # No Color
echo -e "${red}Hello Stackoverflow${NC}"

Q:Doesn‘t work for me -- output: \e[0;31mHello world\e[m

A: Did you try it with "-e"? It tells echo to enable backslash escapes.

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

http://blog.chinaunix.net/uid-1835840-id-2831465.html

http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux

http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html

How to change the output color of echo in Linux,布布扣,bubuko.com

时间: 2024-10-25 18:49:26

How to change the output color of echo in Linux的相关文章

How to change statusbar text color to dark on android 4.4

Because I haven't enough votes, so post picture at here, thank you. Almost 2 weeks ago, I was searching How to change the text color of the statusbar to dark color on android 4.4 The default text color is white, like this I'd like to change the topmo

Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7

Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 Solution Verified - UpdatedOctober 2 2018 at 6:35 AM - https://access.redhat.com/solutions/406773 Environment Red Hat Enterprise Linux (RHEL) 5 Red Hat Enterprise Linu

alias别名、时间、screen、echo等——Linux基本命令(3)

1.alias别名 (1)查看.设置别名 Linux系统提供了一个有用的工具叫alias,可以让我们将一些需要频繁使用的但又过于冗长的命令设置一个别名,这样一来,以后只需输入一个简短的别名就可以达到同样的作用. alias显看当前已定义的别名 使用aliasaliname='command [-x] [....]' 可以定义别名(中括号的意思是可加可不加). 值得注意的是,不止别名=命令,还可以别名=命令+选项 例如,我们设置一个编辑网卡的配置文件别名,然后只需要输入别名,就可以进入了. (2)

java写的 echo 和 linux系统自带的 echo 效率差别很大!

[[email protected] 0]$ uname -o GNU/Linux [[email protected] 0]$ which echo /bin/echo [[email protected] 0]$ time /bin/echo 1234 1234 real 0m0.011s user 0m0.001s sys 0m0.009s [[email protected] 0]$ ls echo.java [[email protected] 0]$ cat echo.java pu

[Angular 2] @Input & @Output Event with ref

The application is simple, to build a color picker: When click the rect box, it will check the color value below and title color will also change. color-picker.ts: import {Component, Output, EventEmitter, Input} from "@angular/core"; import {RED

从CI源码学习PHP高级开发技能——CodeIgniter框架源码深度剖析(4):输出类Output.php

Output类参考说明(摘抄CI手册): 在一般情况下,你可能根本就不会注意到输出类,因为它无需你的干涉, 对你来说完全是透明的.例如,当你使用 加载器 加载一个视图文件时,它会自动传入到输出类,并在系统执行的最后由 CodeIgniter 自动调用.尽管如此,在你需要时,你还是可以对输出进行手工处理. 在说Output类前先说几个知识点和编程技巧: $_SERVER['HTTP_ACCEPT_ENCODING'] .对应请求头是Accept-Encoding:"gzip, deflate&qu

Linux基础命令之echo(涉及bash命令引用及替换部分内容)

echo是Linux中较为常见的命令,特别是在bash的脚本编写中它的灵活运用是必不可少的. 在man手册中的解释是:display a line of text,翻译过来就是回显或者再通俗点说就是显示字符或数字.解释看起来很简单,不过这个命令可以实现的功能却很丰富. 我之所以写关于echo的内容是因为一项作业,作业的内容是让显示一个由任意字符组成的倒三角图形,且要求字符闪烁并有字体颜色跟背景色. 这个作业先搁置一边稍后再解决. 先介绍下echo的基本信息 以下是从CentOS-6.8中的man

Record system output sound in Linux with pacat (Pulseaudio)

Record system output sound in Linux with pacat (Pulseaudio) When pulseaudio is used as the sound server of the system, there is a simple way to record the output sound to file on the command line using the pacat-command. This short article describes

Font Color on a StringGrid with firemonkey

I'm changing the background color based on the data but it makes my text hard to read so I need to change the font color (to white if I have a darker color) but I can't find a way to do it, I'm using Delphi XE8. if not (isSelected) then begin case St