0

I recently saw a trick that let you sort of "generically" output terminal control codes (based on the current termcap/terminfo settings), but can't find it back. If I remember correctly it let you do something like

echo "$(some_command home)"

And what got echoed would be the actual bytes of the escape sequence that would cause one's $TERM to move its cursor home. What was that command?

natevw
  • 184

1 Answers1

1

The command I had seen was tput. One could use it ± as in my question:

echo -n "$(tput home)"

But of course that particular example is redundant; it's wasteful to run the stdout of tput into an argument that echo will write to its own stdout. It might make a little more sense if you needed to generate a string of echo -n "$(tput thing1)some text$(tput thing2)other text — but as @icarus points out in a comment below, this would lose any timing tricks that tput knows about.

So the utility would best be called directly whenever practical:

tput clear
tput blink
echo "DANGER WILL ROBINSON"
tput bel
sleep 5
reset     # NOTE: this might be symlinked to tput also!

It is not a core/built-in tool, but is provided as part of ncurses and should be pretty widely available as a result. I found a nice intro/overview that gives some background, showing related commands as well. And this gist contains a handy listing of all the named "capabilities" that could be output.

natevw
  • 184
  • 1
    In general you don't want the echo -n "$(tput home)", but just use tput home. Whilst uncommon these days tput can be outputting delays which are lost. It is also less efficient to start up a shell to capture the output of a command just to then output it again. – icarus Jan 16 '20 at 00:59
  • @icarus Thanks, hadn't considered the possibility of timing delays within sequences. I updated my answer to be a bit more explicit about this. Also, I added an aside comment about the reset comman —  although on my daily system (macOS) it is symlinked to tset rather than tput. See https://unix.stackexchange.com/questions/335648/why-does-the-reset-command-include-a-delay (via https://unix.stackexchange.com/questions/546918/any-reason-to-not-alias-reset-tput-reset-in-bashrc) for more discussion regarding that side topic. – natevw Jan 16 '20 at 18:21