0

I tried to change the cursor position in the Linux terminal using, for example:

echo -e "\e[16;45H"

However, it did not work as expected. It only moved the cursor on lines and did not move it on columns

What am I doing wrong, and how can I move the cursor to a specific row and column?

1
  • 2
    echo outputs a newline at the end of the arguments you give it, so <CSI>...H<NL> moves to column 1 of the line after the specified line. Commented Oct 21 at 1:23

1 Answer 1

2

There are probably many potential situations that can lead to a problem which could have a similar description. One would be:

If you type that command by itself, and not as part of a script with later echo commands involving plain text, the subsequent shell prompt line will move the cursor.

I would try

 echo -e "\e[16;45Hsome text\e[0;0H"

or

 tput clear cup 16 45; echo "some text"; tput cup 0 0

The advantage of tput is that it uses the terminfo capabilities defined for the terminal type specified in the TERM environment variable. We shouldn't assume everything is ANSI X3.64 (ISO 6429) even though almost everything is nowadays.

4
  • Thanks yes tput works but I have to use ansi coding Commented Oct 20 at 20:54
  • Hi, tput uses ANSI encoding if your TERM is set to xterm, vt100 or any other terminal emulation that uses ANSI. It does exactly the same thing but in a more reliable way. Commented Oct 20 at 20:57
  • If you have to use ANSI coding, then that info needs to be in the question @SorenytMikelyt. Use the EDIT button to make sure your question is as clear and precise as possible. Commented Oct 20 at 22:50
  • tput clear cup 16 45 | od -t x1z will show you a couple of ANSI sequences, with echo $TERM => xterm or a deirivate... so $ CLR="$( tput clear)"; echo $CLR will clear your screen. Commented Oct 21 at 16:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.