For instance, when you pull images from Docker Hub, progress bars (downloading, extracting) in terminal show the progress and are refreshed in accordance with the progress:
9b5a7fa51869: Downloading [==============> ] 8.485MB/29.15MB
9b5a7fa51869: Pull complete
9a72a43d6e84: Extracting [=> ] 2.785MB/89.96MB
I think if you want to print something to the command line, you put content to some channel
or stdout, and it will append to the stream, not update.
I looked up on Internet but I was unable to get any helpful information.
Can you explain how it worked?
CodePudding user response:
If you look at the so-called escape sequences, it is possible to send character sequences which will be interpreted by the terminal as explained
The below script increases the length of an arrow every seconds:
#!/bin/sh
[ $# -ne 1 ] && echo "Usage: `basename $0` arrow_length" >&2 && exit 1
length=$1
while [ $length -gt 0 ]
do
# Move the cursor one step backward to overwrite the preceding arrow's end
echo -ne "\033[1D=>"
sleep 1
length=`expr $length - 1`
done
echo
Example of execution: