clx
clx

Reputation: 824

Set or change vertical position of the cursor

As far as I know, it is possible to move the cursor to the left using the backspace sequence in an echo. But is there any possibility to change the vertical position of the cursor, using an echo?

Upvotes: 24

Views: 34849

Answers (3)

miku
miku

Reputation: 188114

This section describes the ANSI escape sequences:

Examples:

echo -en "\033[s\033[7B\033[1;34m 7 lines down violet \033[u\033[0m"
echo -en "\033[s\033[7A\033[1;32m 7 lines up green \033[u\033[0m"

And this section describes the tput utility:

For a demonstration, see The floating clock in your terminal:

An example script taken from http://www.cyberciti.biz/tips/spice-up-your-unix-linux-shell-scripts.html:

#!/bin/bash

tput clear      # clear the screen

tput cup 3 15   # Move cursor to screen location X,Y (top left is 0,0)

tput setaf 3    # Set a foreground colour using ANSI escape
echo "XYX Corp LTD."
tput sgr0

tput cup 5 17
tput rev        # Set reverse video mode
echo "M A I N - M E N U"
tput sgr0

tput cup 7 15; echo "1. User Management"
tput cup 8 15; echo "2. Service Management"
tput cup 9 15; echo "3. Process Management"
tput cup 10 15; echo "4. Backup"

tput bold       # Set bold mode 
tput cup 12 15
read -p "Enter your choice [1-4] " choice

tput clear
tput sgr0
tput rc

tput example

Upvotes: 33

mr.spuratic
mr.spuratic

Reputation: 10117

Yes, as noted by miku, ANSI terminal escapes, or a little cleaner is tput which should work on any terminal:

tput cuu 1             # move cursor up 1 position
nudgeup=`tput cuu 2`   # save it for later
nudgeleft=`tput cub 2`   
echo -n $nudgeup $nudgeleft

See a couple of sections after the ANSI section in the BASH prompt howto.

tput also returns an exit code that tells you if something is not supported.

Upvotes: 2

kmkaplan
kmkaplan

Reputation: 18960

Using echo will restrict you to a specific terminal type. It is better to use tput.

tput cup 10 4; echo there

will put cursor on row 10 column 4 and print “there” at that position.

For more elementary movements you have tput cub1 to move left, tput cuf1 to move right, tput cuu1 to move up and tput cud1 to move the cursor down.

Upvotes: 9

Related Questions