我知道在 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/