linux 日志颜色修改,如何在linux中更改echo的输出颜色

你可以用最棒的tput命令(建议在伊格纳西奥的回答)为各种事物制作终端控制代码。

使用

专一tput稍后将讨论子命令。

直接

打电话tput作为一系列命令的一部分:tput setaf 1; echo "this is red text"

使用;而不是&&所以如果tput文本仍然显示的错误。

壳变量

另一种选择是使用shell变量:red=`tput setaf 1`green=`tput setaf 2`reset=`tput sgr0`echo "${red}red text ${green}green text${reset}"

tput产生被终端解释为具有特殊意义的字符序列。他们不会被展示出来。请注意,它们仍然可以保存到文件中,或者由终端以外的程序作为输入处理。

命令替换

插入可能更方便tput的输出直接进入echo字符串使用命令替换:echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

前景和背景颜色命令tput setab [1-7] # Set the background colour using ANSI escapetput setaf [1-7] # Set the foreground colour using ANSI escape

颜色如下:Num  Colour    #define         R G B

0    black     COLOR_BLACK     0,0,0

1    red       COLOR_RED       1,0,0

2    green     COLOR_GREEN     0,1,0

3    yellow    COLOR_YELLOW    1,1,0

4    blue      COLOR_BLUE      0,0,1

5    magenta   COLOR_MAGENTA   1,0,1

6    cyan      COLOR_CYAN      0,1,1

7    white     COLOR_WHITE     1,1,1

还有非ANSI版本的颜色设置功能(setb而不是setab,和setf而不是setaf)使用不同的数字,而不是在这里给出。

文本模式命令tput bold    # Select bold modetput dim     # Select dim (half-bright) modetput smul    # Enable underline modetput rmul

# Disable underline modetput rev     # Turn on reverse video modetput smso    # Enter standout (bold) modetput rmso

# Exit standout mode

光标移动命令tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)tput cuf N   # Move N characters forward (right)tput cub N

# Move N characters back (left)tput cuu N   # Move N lines uptput ll      # Move to last line, first column (if no cup)tput sc

# Save the cursor positiontput rc      # Restore the cursor positiontput lines   # Output the number of lines of the terminaltput cols

# Output the number of columns of the terminal

清除和插入命令tput ech N   # Erase N characterstput clear   # Clear screen and move the cursor to 0,0tput el 1

# Clear to beginning of linetput el      # Clear to end of linetput ed      # Clear to end of screentput ich N

# Insert N characters (moves rest of line forward!)tput il N    # Insert N lines

其他命令tput sgr0    # Reset text format to the terminal's defaulttput bel     # Play a bell

带着车窗摇摇晃晃,bel命令使终端抖动一秒钟,以引起用户的注意。

剧本

tput接受每行包含一个命令的脚本,这些命令是按照前面的顺序执行的。tput出口。

通过回显多行字符串并对其进行管道处理,可以避免临时文件:echo -e "setf 7\nsetb 1" | tput -S  # set fg white and bg red

另见看见有关这些选项的完整命令列表和更多详细信息。(对应

tput命令列在

Cap-name)从第81行开始的巨型表的列。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值