Shell script to identify console color output

I need to determine if my terminal outputs colored output or not using bash scripts. Is there any direct shell command for this? How can i do this?

My main goal is to determine if the result matches the terminal color by default or not. If it does not match, I must write a warning message in a text file.

+4
source share
2 answers

Control characters also output characters, so you may find their sequences similar to this answer .

if printf "\x1b[31mRed\x1b[0m" | grep -Pq "\\x1b\[[0-9;]+m"; then
  echo colored
else
  echo uncolored
fi
  • printf . echo, -e.
  • grep -q 0 (), , ( ).
  • -P ( PERL) grep , POSIX escape-. \\x1b, grep escape . Perl GNU grep, , , BSD ( Mac OS X).

, , tty-, script , , .

+1

, , - Escape Sequence,

# red
awk '/\033\[31m/'

0

All Articles