例如, 我每秒都输出
user@pc:
A
B
C
D
E
我希望在同一行中从A到E的输出刷新(不创建新行)
谢谢!
答案 0 :(得分:2)
您可以使用carriage return
字符" \r
"。检查之间的区别:
for x in $(seq 10); do printf "$x"; sleep 1; done
和
for x in $(seq 10); do printf "$x\r"; sleep 1; done
答案 1 :(得分:0)
取决于您输入的来源。
我会采用以下技巧。
对于文件:
#!/bin/bash
while read line
do
echo -e "\e[1A" # moving the cursor back to the previously printed line
sleep 1s;
echo -ne "$line\e[K" # \e[K cleans the residues of the previous output.
done <file_name
echo
对于数组:
#!/bin/bash
arr=(12 11 10 9 8 7 6 5 4 3 2 1 0)
for i in ${arr[@]}
do
echo -e "\e[1A"
sleep 1s;
echo -ne "Waiting time : "$i" Seconds\e[K"
done
echo