Hello Dan,

Dan wrote on Tue, Aug 05, 2025 at 01:51:59AM +0200:

> Scripting under ksh: does it exist a way to script a clear of the last
> three printed chars on the current line ?

   $ printf "this is a test\b\b\b   \n"
  this is a t
   $ printf "this is a test"; printf "\b\b\b   "; printf "\n"
  this is a t
   $ printf "this is a test"; sleep 10; printf "\b\b\b   "; printf "\n"
  this is a test <- cursor remains here
  [10 seconds later, the output line changes to:]
  this is a t
   $

This is actually quite close to how the C code implementing ksh(1)
does this internally when it wants to delete the last three characters
printed: it simply write(2)s three backspace characters followed
by three space characters to standard output.  Of course, that's
where the similarity ends; internally, it obviously does not use
printf(1) or echo(1) or anything similar.  Just write(2).

That said, if you want to do terminal manipulation of this kind,
then you almost certainly do not want to write your program in
the ksh(1) language.  Instead, you want to write your program
in C and use curses(3) for cursor positioning and selective
updates of terminal window content.  Or in another high-level
language that provides terminal manipulation facilities.

Any *sh(1) scripting language is a very poor fit for writing
a program that wants to perform terminal manipulation.
Generally, do not write any program of significant size in *sh(1).
It is among the hardest languages to use securely; certainly,
writing good sh(1) or ksh(1) code is *much* harder than
writing good C code - so much so that i have already rewritten
some ksh(1) programs in perl(1) for the OpenBSD project to
make them more secure and less fragile.  Writing good perl(1)
code is certainly harder to learn than writing good C code,
but perl(1) is still easier to use securely than *sh(1).

Yours,
  Ingo

Reply via email to