Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo strings at the same position?

Tags:

bash

shell

echo

I built my web server and I'm trying to do a test. So I simulate many requests with bash script:

i=0
while [ $i -lt 20 ]; do
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done

This works well but I prefer to make all of echo at the same position on the terminal.
I've read this: How to show and update echo on same line

So I add -ne for echo but it doesn't seem to work as expected.
The messages of curl can still push the echo away.

This is what I need:

============== current time =============== ---\
1   <------ this number keeps updating      ----> the 3 lines stay here
=========================================== ---/
Here is the messages of `curl`, which are showing as normal way
like image 855
Yves Avatar asked Sep 06 '25 01:09

Yves


1 Answers

There's another option, to position the cursor before you write to stdout.

You can set x and y to suit your needs.

#!/bin/bash

y=10
x=0
i=0
while [ $i -lt 20 ]; do
    tput cup $y $x
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done
like image 62
Diego Torres Milano Avatar answered Sep 09 '25 22:09

Diego Torres Milano