Linux基础命令 您所在的位置:网站首页 彩色脚本 Linux基础命令

Linux基础命令

2024-06-11 14:29| 来源: 网络整理| 查看: 265

描述

在ANSI/VT100 终端和终端仿真器不只是能够显示黑色和白色文本;由于转义序列,它们可以显示颜色和格式化的文本。 这些序列由转义字符 (通常由^[或表示)后接其他一些字符:FormatCode组成。[FormatCodem

在 Bash, 字符可以使用以下语法获取字符:

\e\033\x1B

实例:

代码(Bash)预览echo -e “ \ e [31mHello World \ e [0m”在这里插入图片描述echo -e “ \ 033 [31mHello \ e [0m World”在这里插入图片描述 注意: 该命令的-e选项echo启用对转义序列的解析。\e[0m序列删除所有属性(格式和颜色)。在每个彩色文本的末尾都最好添加它。 说明: \e 转义起始符,定义一个转义序列, 可以使用 \033或者\x1B代替[ 表示开始定义颜色m 转义终止符,表示颜色定义完毕再次使用 \e[ ,表示再次开启颜色定义,0表示使用默认的颜色,m表示颜色定义结束,所以 \e[0m 的作用是恢复之前的配色方案 格式化(Formatting) 描述: 这是最常用的用于格式化文本的控制序列。它们的支持取决于所使用的终端(请参阅兼容性列表)。 集合(SET) 编码描述实例预览1粗体/明亮(Bold/Bright)echo -e "Normal \e[1mBold"在这里插入图片描述2暗淡(Dim)echo -e "Normal \e[2mDim"在这里插入图片描述4下划线(Underlined)echo -e "Normal \e[4mUnderlined""在这里插入图片描述5闪烁(Blink)1echo -e "Normal \e[5mBlink"不闪烁在这里插入图片描述7反转(反转前景色和背景色)echo -e "Normal \e[7minverted"在这里插入图片描述8隐藏(对密码有用)echo -e "Normal \e[8mHidden"在这里插入图片描述 重置(Reset) 编码描述实例预览0重置所有属性echo -e "\e[0mNormal Text"在这里插入图片描述21重设粗体/明亮echo -e "Normal \e[1mBold \e[21mNormal"在这里插入图片描述22重设暗淡echo -e "Normal \e[2mDim \e[22mNormal"在这里插入图片描述24重设下划线echo -e "Normal \e[4mUnderlined \e[24mNormal"在这里插入图片描述25重设闪烁echo -e "Normal \e[5mBlink \e[25mNormal"这里不能显示动图在这里插入图片描述27重设反转echo -e "Normal \e[7minverted \e[27mNormal"在这里插入图片描述28重设隐藏echo -e "Normal \e[8mHidden \e[28mNormal"在这里插入图片描述 8/16 色 前景色(文字) 编码描述实例预览39默认前景色echo -e "Default \e[39mDefault"在这里插入图片描述30黑色echo -e "Default \e[30mBlack"在这里插入图片描述31红色echo -e "Default \e[31mRed"在这里插入图片描述32绿色echo -e "Default \e[32mGreen"在这里插入图片描述33黄色echo -e "Default \e[33mYellow"在这里插入图片描述34蓝色echo -e "Default \e[34mBlue"在这里插入图片描述35品红echo -e "Default \e[35mMagenta"在这里插入图片描述36青色echo -e "Default \e[36mCyan"在这里插入图片描述37浅灰echo -e "Default \e[37mLight gray"在这里插入图片描述90深灰色echo -e "Default \e[90mDark gray"在这里插入图片描述91淡红色echo -e "Default \e[91mLight red"在这里插入图片描述92浅绿色echo -e "Default \e[92mLight green"在这里插入图片描述93浅黄色echo -e "Default \e[93mLight yellow"在这里插入图片描述94淡蓝色echo -e "Default \e[94mLight blue"在这里插入图片描述95淡品红echo -e "Default \e[95mLight magenta"在这里插入图片描述96浅青绿echo -e "Default \e[96mLight cyan"在这里插入图片描述97白色echo -e "Default \e[97mWhite"在这里插入图片描述 背景(Background) 编码描述实例预览49默认背景色echo -e "Default \e[49mDefault"在这里插入图片描述40黑色echo -e "Default \e[40mBlack"在这里插入图片描述41红色echo -e "Default \e[41mRed"在这里插入图片描述42绿色echo -e "Default \e[42mGreen"在这里插入图片描述43黄色echo -e "Default \e[43mYellow"在这里插入图片描述44蓝色echo -e "Default \e[44mBlue"在这里插入图片描述45品红echo -e "Default \e[45mMagenta"在这里插入图片描述46青色echo -e "Default \e[46mCyan"在这里插入图片描述47浅灰echo -e "Default \e[47mLight gray"在这里插入图片描述100深灰色echo -e "Default \e[100mDark gray"在这里插入图片描述101浅红色echo -e "Default \e[101mLight red"在这里插入图片描述102浅绿色echo -e "Default \e[102mLight green"在这里插入图片描述103浅黄色echo -e "Default \e[103mLight yellow"在这里插入图片描述104浅蓝echo -e "Default \e[104mLight blue"在这里插入图片描述105浅洋红色echo -e "Default \e[105mLight magenta"在这里插入图片描述106浅青色echo -e "Default \e[106mLight cyan"在这里插入图片描述107白色echo -e "Default \e[107mWhite"在这里插入图片描述 88/256色 说明: 某些终端(请参阅兼容性列表)可以支持88或256种颜色。这是允许您使用它们的控制序列。颜色数字256仅受vte支持(GNOME终端,XFCE4终端,Nautilus终端,终结器等)88色终端(如rxvt)与256色终端的色图不同。要显示88色终端的颜色图,请在88色终端中运行256-colors.sh脚本。 前景色(文字)

要使用前景上的256种颜色(文本颜色)中的一种,控制顺序为[38;5;ColorNumberm,其中ColorNumber是以下颜色之一: 在这里插入图片描述

实例: 代码预览echo -e “ \ e [38; 5; 82mHello \ e [38; 5; 198mWorld”在这里插入图片描述for i in {16..21} {21..16} ; do echo -en "\e[38;5;${i}m#\e[0m" ; done ; echo在这里插入图片描述 背景色

要在背景上使用256种颜色之一,控制顺序为[48;5;ColorNumberm,其中ColorNumber是以下颜色之一: 在这里插入图片描述

实例: 代码预览echo -e "\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m"在这里插入图片描述for i in {16..21} {21..16} ; do echo -en "\e[48;5;${i}m \e[0m" ; done ; echo在这里插入图片描述 属性组合

终端允许属性组合。属性必须用分号(“ ;”)分隔。

实例: 描述代码预览加粗加下划线echo -e "\e[1;4mBold and Underlined"在这里插入图片描述粗体+红色前景+绿色背景echo -e "\e[1;31;42m Yes it is awful \e[0m"在这里插入图片描述 demo程序(Demonstration programs) 颜色和格式(16种颜色)

以下Shell脚本显示了许多可能的属性组合(但不是全部,因为它一次只使用一个格式属性)。 点击下载: colors_and_formatting.sh

#!/bin/bash # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. #Background for clbg in {40..47} {100..107} 49 ; do #Foreground for clfg in {30..37} {90..97} 39 ; do #Formatting for attr in 0 1 2 4 5 7 ; do #Print the result echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m" done echo #Newline done done exit 0

在这里插入图片描述

256 色

以下脚本显示了某些终端和终端仿真器(例如XTerm和GNOME Terminal)上可用的256种颜色。 点击下载:256-colors.sh

#!/bin/bash # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. for fgbg in 38 48 ; do # Foreground / Background for color in {0..255} ; do # Colors # Display the color printf "\e[${fgbg};5;%sm %3s \e[0m" $color $color # Display 6 colors per lines if [ $((($color + 1) % 6)) == 4 ] ; then echo # New line fi done echo # New line done exit 0

在这里插入图片描述

不适用于大多数的终端仿真器,可在tty和Term中使用 ↩︎



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有