linux - grep 是如何制作颜色的?

标签 linux bash redirect colors grep

我知道在 bash 中我可以打印彩色字符串,例如:

echo -e "\033[33;1mhello\033[0m"

shell 中的输出将是金色的 hello。但是当我将输出重定向到文件 test.txt 时,\033[33; 也会出现在文本文件中。但是 grep --color=auto 命令不会将这些字符重定向到文本文件中。它怎么能做到这一点?

最佳答案

这个怎么样?

#!/bin/bash

if [ -t 1 ]; then
    echo -e "\033[33;1mhello\033[0m"
else
    echo hello
fi

这里是解释:

test -t <fd> , 其简称为 [ -t <fd> ] , 检查描述符是否 <fd>是否是终端。来源:help test

关于linux - grep 是如何制作颜色的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568426/

相关文章:

java - 内存利用率

linux - 复制路径中的文本 (Bash)

magento - Magento 设置的 302 重定向,9300 抓取错误

linux - sed 在 crontab 中添加新的注释行

javascript - 使用 IF 更改元素位置

c# - 检测 Webbrowser 是否重定向

linux - unix & gnu/linux 发行版 : possible locations for 'which' command?

linux - 仅通过文件名检查文件是否存在(不带扩展名)

bash - 仅按名称列出 Gitlab 运行器

linux - 用于在 Linux 中的所有子文件夹中使用正确的日期格式重命名文件名的 Bash 脚本