As the other said you should use echo
or printf
with carriage return \r
.
So what you want is to print only 5 chars per loop you could use the script below for that behavior:
#!/bin/bash
null=$'\0'
text="Hi! How are you doing on this fine evening? ${null}"
#while :; do
count=0
for (( i = 0; i < "${#text}"; i++ )); do
printf "\r%s" "${text:$i:5}"
sleep 0.3
done
#done
echo
With this line printf "\r%s" "${text:$i:5}"
I'm printing 5 characters per loop, where the $i
is the current index and the 5
is the length of the string to print.
For example, if you print the text
variable as follows:
printf "%s" "${text:0:5}"
#Output
Hi! H
printf "%s" "${text:1:5}"
#Output
i! Ho
#and so on
If you wish to print all the string and remove each character from left per loop you can use this script:
#!/bin/bash
null=$'\0'
text="Hi! How are you doing on this fine evening? ${null}"
#while :; do
count=0
for (( i = 0; i < "${#text}"; i++ )); do
printf "\r%s" "${text:$i}"
sleep 0.3
done
#done
echo
The code above will print:
In first loop:
Hi! How are you doing on this fine evening?
In second loop:
i! How are you doing on this fine evening?
In third loop:
! How are you doing on this fine evening?
and so on.
Note: The variable $null
should be used to avoid printing at the end of the string its last character, in this case ?
. And if you want a infinite loop your should uncomment the lines #while :; do
and #done
.
\r
is probably your friend here. What have you already tried?