Echo backspace in spinner script

Trying to insert a backspace for echo.

while true ; do
i='-\|/'
for j in 1 2 3 4
do
echo -ne "$(echo ${i} | cut -c${j})"
sleep 1
done
done

this currently outputs:
-\|/-\|/-\|/-\| .....etc

---------- Post updated at 02:34 PM ---------- Previous update was at 02:10 PM ----------

i resolved the issue myself using replacing echo with printf

while true ; do
i='-/|\'
for j in 1 2 3 4
do
printf "%c\r $(echo ${i} | cut -c${j})"
sleep 0.25
done
done

Alternatively you can use ^H (ctrl+v backspace) as a backspace character.

Your version of echo might support \b , in which case you might try:-

while true
do
   i='-\|/'
   for j in 1 2 3 4
   do
      echo -ne "\b$(echo ${i} | cut -c${j})"
      sleep 1
   done
done

I hope that this helps,

Robin
Liverpool/Blackburn
UK

for i in \- / \| \\
do
printf "%c\r" "$i"
done
indi=( "/" "-" "\\" "|" )
C=0
while [ true ]
do    printf "\b${indi[$C]}"
      let C++
      [[ $C -gt 3 ]] && C=0
done

hth

SP='-/|\'
while true; do printf "\b%s" ${SP:i++%4:1}; sleep 0.1; done

You might like... ;o)
Much related you could try this as an added extra:-

Last login: Mon Sep 30 19:34:05 on ttys000
AMIGA:barrywalker~> clear; star=""; i='-\|/*'; for k in {0..9}; do for j in {0..4}; do clear; printf "\n\n\n\n\n          $star${i:$j:1}"; sleep 0.25; done; star="$star"'*'; done; printf "\n\n"
1 Like

How about another variation:-

row=0
while true
do
   tput clear
   until [ $row -ge 23 ]
   do
      col=0
      until [ $col -ge 80 ]
      do
         for char in "\\" "|" "/" "-" "*"
         do
            tput cup $row $col
            echo "$char"
            tput cup 24 0
            sleep 1
         done
         ((col=$col+1))
      done
      col=0
      ((row=$row+1))
   done
   row=0
done

This leave the cursor on the bottom line (of a 24x80 screen) after each character so that any messages could be displayed and not immediately overwritten.

Robin