How do I determine if a terminal is color-capable?

A friend pointed me towards tput(1), and I cooked up this solution:

#!/bin/sh

# ack-wrapper - use tput to try and detect whether the terminal is
#               color-capable, and call ack-grep accordingly.

OPTION='--nocolor'

COLORS=$(tput colors 2> /dev/null)
if [ $? = 0 ] && [ $COLORS -gt 2 ]; then
    OPTION=''
fi

exec ack-grep $OPTION "$@"

which works for me. It would be great if I had a way to integrate it into ack, though.

Leave a Comment