Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that tput sc saves the current cursor position and tput rc restores it exactly where tput sc was called. The problem is that every time tput sc is called, it overwrites the previous saved position.

Is there a way to save more positions, e.g. tput sc pos1 and tput sc pos2 which can be restored with, say, tput rc pos1 and tput rc pos2 respectively? (The solution need not make use of tput, I mentioned it because it's the only command I know that handles cursor position)

If not, is there a way to at least save the cursor position locally in a function, so that if a function uses tput sc and then calls another function that runs again tput sc, then each function restores its own saved cursor position when invoking tput rc?

Thanks in advance.

share|improve this question
up vote 5 down vote accepted

You can use the following function to extract current cursor position in a simple array:

extract_current_cursor_position () {
    export $1
    exec < /dev/tty
    oldstty=$(stty -g)
    stty raw -echo min 0
    echo -en "\033[6n" > /dev/tty
    IFS=';' read -r -d R -a pos
    stty $oldstty
    eval "$1[0]=$((${pos[0]:2} - 2))"
    eval "$1[1]=$((${pos[1]} - 1))"

(the source of code used in this function was taken and adapted from this answer)

Now, for example, to save current cursor position in pos1, use:

extract_current_cursor_position pos1

To save current cursor position in pos2, use:

extract_current_cursor_position pos2

To see cursor positions saved in pos1 and pos2, you can use:

echo ${pos1[0]} ${pos1[1]}
echo ${pos2[0]} ${pos2[1]}

To move/restore cursor position to pos1, you have to use:

tput cup ${pos1[0]} ${pos1[1]}

To move/restore cursor position to pos2, you have to use:

tput cup ${pos2[0]} ${pos2[1]}
share|improve this answer
Works great, thanks! – AndreasT Oct 26 '13 at 15:47

tput command works through terminal control sequences, listed here: There is a sequence for extracting current position (Query Cursor Position - \e[6n) and looks like it's not present in tput. To extract it use:

stty -echo; echo -n $'\e[6n'; read -d R x; stty echo; echo ${x#??}

Now you can extract row position saved in $x to some other variable and move your cursor using tput cup later:

$ echo $my_saved_pos
$ tput cup $my_saved_pos 0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.