highlighting text in shell
The escape sequences for that may be terminal specific. That's the whole point of using tput
. tputs looks up the correct escape sequence in a database based on the value of the $TERM
variable.
On my terminal:
$ tput smso | sed -n l
\033[3m$
$ tput rmso | sed -n l
\033[23m$
So I could do:
$ printf '\033[3m%s\033[23m\n' "stand out"
But I can't be sure that would work on other terminals.
If you don't want to call tput
each time, you can run it once and store the output:
smso=$(tput smso) rmso=$(tput rmso)
printf '%s\n' "${smso}stand out${rmso}"
Note that smso
is "Start Mode Stand-Out", it is not for reverse video, though many terminals use reverse video to make text stand out. If you want reverse video, it's tput rev
(cancelled by tput sgr0
), if you want to set the background colour, use tput setab 4
for ANSI colour codes (with 4 being the ANSI colour blue number).
Related videos on Youtube
Comments
-
g4ur4v over 1 year
I have always been using below method to highlight background of text in shell .
tput smso;printf " TEXT ";tput rmso;
How can I achieve the same thing without using
tput
( I mean some way formatting like\e[0m
for colors in printf) ?-
BitsOfNix almost 11 years
-
g4ur4v almost 11 yearsI am looking for code for highlighting text similar to the ones used for coloring.This link talks about only colors.
-
BitsOfNix almost 11 yearswell, you need to know the correct escape codes and before the text you puts your coloring and after you remove. Here you have a table of the combinations: bitmote.com/public/ansi_4bit_color_table.png printf works with the escapes code that you mentioned on your question.
-
tripleee almost 11 yearsWhy would you trade a portable and somewhat readable technique for a nonportable unreadable one? If you want to save processes, maybe
p="$(tput smso)"; q="$(tput rmso)"
when you initialize, then useprint "${p}can i haz cheeseburger?${q}"
-