-1

I want to do something like this:

user@myserver$echo -en "\rSome text to overwrite same line command is on"

The problem is I end up with this:

Some text to overwrite same line command is onuser@myserver$

If I do echo -e only then I get this:

Some text to overwrite same line command is on

user@myserver$

Which is okay except it causes the console to scroll. I don't want it to scroll because my console window with tmux is only one line high. I've tried padding the right hand side of the string with spaces but that doesn't work correctly.

dpetican
  • 277
  • 3
  • 12
  • Yes except the correct and most useful answer so far did not use escape sequences. Just because I was "thinking about using escape sequences" doesn't imply that I wanted the solution to include only escape sequences. Therefore I will remove that musing. – dpetican Sep 08 '16 at 20:22
  • What do you want to do? You've shown two examples of things that you say aren't right, but I see nothing that shows what you consider to be desired behaviour – roaima Sep 08 '16 at 21:20
  • There is a bit of an XY problem here. I gather the end goal was to keep the output of a command from being scrambled by the shell's prompt, but the question title (still) refers specifically to escape codes, which would be more of a tool, and maybe not the best for this particular end. – ilkkachu Sep 08 '16 at 23:07

2 Answers2

2
$ echo -en "\rSome text"

Here, the \r doesn't do much, since it returns the cursor to the start of the line, but after you hit enter on the command line, the cursor already is at the start of the line following the prompt. As in just a simple echo, the output is on a line of its own:

$ echo foo
foo

So you can't well overwrite the same command line (at least without scrolling a new one in before that). But you don't need to, if you only care about the last line showing. You just need to prevent the next prompt from printing. You could use read to wait for some input (hitting enter) before continuing. The cursor stays at the _ after the bar.

$ echo -n foo bar ; read
foo bar_
ilkkachu
  • 133,243
  • 15
  • 236
  • 397
0

try

echo -en "\rSome text to overwrite same line command is on\n"
Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227