2

There is a color codes script on GitHub suitable for sourcing, which I am modifying for my own purposes.


Background

I have an odd mix of environments to work in, from a Mac OS terminal, to various Linux distributions, and even to a MobaXterm on my Windows work laptop. I'd like to get the color codes to a maximum portability for these environments

In MobaXterm, tput setaf 2 (for example) doesn't work at all—it just says "Segmentation fault," though I see no ill effects. Likewise, echo '\e[4;32m' doesn't work either; it just displays those literal characters. (This is likely because it is BusyBox echo.)

printf '\e[4;32m' works, though.


The question:

Is there any reason not to use printf in place of tput in all cases?

Or put another way:

Is there any environment (or OS or shell) in which printf WON'T work to set terminal colors, but tput WILL?

Wildcard
  • 35,316
  • 26
  • 130
  • 258
  • `tput` looks up the correct codes for your terminal (these days, they're nearly all vt-100 compatible to varying degrees) in the `terminfo` or `termcap` database and uses them. Using `printf` or `echo` or whatever to output colour codes is hard-coding them so that only work on the current terminal type (or compatible). In this modern era that often doesn't matter (due to the near-universal use of vt-100 compatible terminals), but some people still use non-vt100 terminals or terminal emulators....anyway, hard-coding is pure evil. – cas May 21 '16 at 01:01
  • btw, what's in your `TERM` and/or `TERMCAP` variables when logging in via MobaXterm? It may be that your terminfo db has a bad (or non-existent?) entry for that $TERM. Does `tput` work in Moba if you do something like `export TERM=xterm`? – cas May 21 '16 at 01:05
  • @cas, `echo $TERM` shows `xterm` already. – Wildcard May 21 '16 at 01:10
  • I would guess that mobaxterm probably doesn't implement an xterm correctly. Have you tried telling it to emulate different terminal types? Perhaps vt-102 or vt-220? – cas May 21 '16 at 01:13

0 Answers0