On the subject of optimizing tput, this article mentions using hard-coded strings. Another approach (for some tput commands) is to run the command once, save its output in a variable, and re-use it.<p>For example, a slow version of printing a blank chess board:<p><pre><code> #! /bin/sh
for rowpair in 1 2 3 4
do
for colpair in 1 2 3 4
do
echo -n " $(tput rev) $(tput sgr0)"
done
echo
for colpair in 1 2 3 4
do
echo -n "$(tput rev) $(tput sgr0) "
done
echo
done
</code></pre>
And a faster version:<p><pre><code> #! /bin/sh
rev=$(tput rev)
sgr0=$(tput sgr0)
for rowpair in 1 2 3 4
do
for colpair in 1 2 3 4
do
echo -n " $rev $sgr0"
done
echo
for colpair in 1 2 3 4
do
echo -n "$rev $sgr0 "
done
echo
done
</code></pre>
In many cases, tput is doing doing anything more than looking up a string and printing it. Though in other cases like "tput cols", it is doing more. (And anyway, the number of columns isn't a constant.)