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) ?

  • 2
  • I am looking for code for highlighting text similar to the ones used for coloring.This link talks about only colors.
    – g4ur4v
    Jul 23 '13 at 9:01
  • 1
    well, 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.
    – BitsOfNix
    Jul 23 '13 at 9:17
  • 3
    Why 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 use print "${p}can i haz cheeseburger?${q}"
    – tripleee
    Jul 23 '13 at 12:45

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
$ tput rmso | sed -n l

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.