What I do not understand and what makes your postings confusing imho is the mixing of methods in one and the same program line. I programmer should use a style of programming and stick to that. Else he will never deliver neat and understandable code (not understandable even by himself).
Thus why in your
echo -en "\033[2K\r
you use octal for one and special character for the other. Either do
echo -en "\E[2K\r"
or do
echo -en "\033[2K\015"
And why the mix of printf and echo. When you want to test this, it is simple:
henk@boven:~> echo -en aap "\E[2K\r"
henk@boven:~>
Also do not only show what statements you have, but also what the result is. And explain where that result differs from what you want:
henk@boven:~> echo -e 'fooooo\r \rbar'
bar o
henk@boven:~>
which is what I would expect:
[ol]
[li]fooooo is send to the terminal and displayed from where the cursor is; [/li][li]CR makes the cursor go back to the begin of the line; [/li][li]5 spaces are written to the line, overwriting the first five characters foooo and leaving the fifth o; [/li][li]CR makes the cursor go back to the begin of the line; [/li][li]the characters bar are written to the line, overwriting the first 3 spaces, leaving two spaces and the o; [/li][li]this being the end of the echo command a LF is written (no -n option on the echo command) and the cursor thus steps down one line; [/li][li]the shell takes over and displays a prompt. [/li][/ol]
And that is what I see. So no complaints or amazement from me. But how about you?
Same for
printf "$S" $string1; echo -en "\033[2K\r"
You only show a statement, but you do not explain what you get. nor what you expected to get. We are no mindreaders! Simply saying “I can’t get the esc sequence to work.” tells us almost nothing.
And of course what avidjaar says: changing a process environment variable does not change your (emulated) hardware.
Try to understand. You have a piece of hardware (a character termminal at the end of an asynchronous line) that is wired to understand and act upon certain character combinations (“Escape sequences”) that are send to it over the line from the computer. You send those sequences and the terminal does what it should do. Only be sure that the terminal is capable to understand these VT100/ANSI codes (and Konsole and most other terminal emulators can do that).
Now these days the terminal has changed to a terminal emulator (e.g. Konsole) and the asynchronous line has shrunken to nothing, but it works still the same:
[ul]
[li]when you send the character A, the terminal will show the shape of an A where the cursor is and step the cursor one step forward; [/li][li]when you send the characters Esc[2K the terminal will put the cursor back to the begin of the line the cursor is on and clear everything on that line; [/li][li]and so on [/li][/ul]
All very basic, existing for over 30 years.