Skip to content

Instantly share code, notes, and snippets.

@jrbalsano
Created May 28, 2013 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrbalsano/5664675 to your computer and use it in GitHub Desktop.
Save jrbalsano/5664675 to your computer and use it in GitHub Desktop.
An example of how to overwrite lines in the shell using a few different techniques. For further explanation: http://blog.jrbalsano.com/post/51571798768/overwriting-multiple-lines
#!/bin/sh
#Example 1 - Overwrite a line with a longer line
echo "This is a line\c"
sleep .5
echo "\rThis is another line, overwriting the last one."
#Example 2 - Overwrite a line with a shorter line
echo "This is a much longer line than the one we're about to write\c"
sleep .5
echo "\rThis is a short line"
#Example 3 - Erase a line
echo "This line is about to vanish\c"
sleep .5
echo $(tput el1)
sleep .5
#Example 4
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $(tput el1) "\r\c"
for((j = 0; j<i; j++))
do
echo "=\c"
done
sleep .5
done
#Example 5
BLANKLINE=$(tput el1)\\r\\c
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$BLANKLINE"
for((j = 0; j<i; j++))
do
echo "=\c"
done
sleep .5
done
#Example 6
ERASELINE=$(tput el)
UPLINE=$(tput cuu1)
PROGRESS=""
echo "\n" # Add two blank lines to erase over. (One from \n, one from echo)
for i in a b c d e f g h i j
do
PROGRESS+="="
echo "$UPLINE$ERASELINE$UPLINE$ERASELINE\c" #Go up two lines, erasing them
echo $i
echo $PROGRESS
sleep .5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment