I want to write a shell script that will use colored output when output is terminal, and normal output when redirected to a file. How can I do that?
我想编写一个shell脚本,当输出为终端时将使用彩色输出,并在重定向到文件时使用正常输出。我怎样才能做到这一点?
1 个解决方案
#1
9
Very simple:
if [ -t 1 ]; then
echo "Hello, terminal."
else
echo "Not a terminal."
fi
-t
tests if the given file descriptor (here, 1 = stdout) is attached to a terminal.
-t测试给定的文件描述符(此处为1 = stdout)是否附加到终端。
#1
9
Very simple:
if [ -t 1 ]; then
echo "Hello, terminal."
else
echo "Not a terminal."
fi
-t
tests if the given file descriptor (here, 1 = stdout) is attached to a terminal.
-t测试给定的文件描述符(此处为1 = stdout)是否附加到终端。