The command tput can be used for many awesome things.
E.g. determine what $COLUMNS your current terminal has to properly word-wrap a long line of output text:
Without fold and tput
Code: Select all
#!/bin/sh
cat <<EOF
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOF
result:
Code: Select all
guest@rava:/tmp$ ./lore.sh
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
guest@rava:/tmp$
With fold and tput
Code: Select all
#!/bin/sh
cat <<EOF|fold -sw $(tput cols)
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOF
result:
Code: Select all
guest@rava:/tmp$ ./lorefold.sh
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor
incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat.
Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa
qui officia deserunt mollit anim id est laborum.
guest@rava:/tmp$
Much better.
But to be more precise, on a 80 characters $COLUMNS terminal it does look like so when executing "./lore.sh"
Code: Select all
guest@rava:/tmp$ ./lore.sh
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incid
unt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exer
citation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. E
xcepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deseru
nt mollit anim id est laborum.
guest@rava:/tmp$
Not really pretty.
Conclusion:
OUTPUT TO STANDARD |fold -sw $(tput cols) is nice.
But I already mentioned that somewhere above, and
tput and/or printf can do much more.
Like colours.
Try
vs
Now you might ask yourself "tput setaf 1" is nice and all, and "tput setaf 1" seems to translate into "red", but what with the other colours?
Fret not,
unix.stackexchange.com comes to the rescue.
This very post
https://unix.stackexchange.com/question ... olor-codes to be precise.
Have some code snippets from some answers on there:
Code: Select all
#!/bin/bash
color(){
for c; do
printf '\e[48;5;%dm%03d' $c $c
done
printf '\e[0m \n'
}
IFS=$' \t\n'
color {0..15}
for ((i=0;i<6;i++)); do
color $(seq $((i*36+16)) $((i*36+51)))
done
color {232..255}

(screenshot from the
https://unix.stackexchange.com/questions/269077/ article, it looks slightly different when executed in my xfce4-terminal (mainly because I use 80 characters per line), but basically the output is the same; the "015" in the first line is not visible to me, though: in my terminal that is
white text on white background 
)
To print all 256 colours in your terminal, try the following rainbow-y one-liner:
Code: Select all
for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done
or
Code: Select all
for c in {0..255}; do tput setaf $c; tput setaf $c | cat -v; echo =$c; done| column
I hope you can use this to make your scripts more easily readable compared to an all-one-text-coloured-only output.
Added in 6 hours 58 minutes 31 seconds:
Silly me forgot to add the basic 8 colours table from the very same article as quoted and linked above, so here you go:
Code: Select all
Color #define Value RGB
black COLOR_BLACK 0 0, 0, 0
red COLOR_RED 1 max,0,0
green COLOR_GREEN 2 0,max,0
yellow COLOR_YELLOW 3 max,max,0
blue COLOR_BLUE 4 0,0,max
magenta COLOR_MAGENTA 5 max,0,max
cyan COLOR_CYAN 6 0,max,max
white COLOR_WHITE 7 max,max,max
As you might have seen, the attribute "bold" is missing in that article. Another article explains why:
https://unix.stackexchange.com/question ... codes-bold
Bold is a character-related video attribute, usually thicker lines, but which in some devices is imitated using bright colors (a change in intensity).
RGB already gives the color intensity, so at most you'll find some terminals doing something like changing the levels a little — but nothing that you can count on in different terminals.