printf "line one\n"
printf "line two\n"
这些打印输出之后,我想替换第一行并打印其他内容
(不使用clear
)。我已经尝试过以下命令:
printf "line one\n"
printf "line two\r"
这不是我想要的,因为它代替了最后一行line two
,而不是line one
。
我想做什么:
printf "line one\n"
printf "line two\n"
sleep 0.5
somecode "line three"
我想要的输出:
line three
line two
答案 0 :(得分:5)
这可以通过flatMap
完成,例如:
tput
最初将打印:
EraseToEOL=$(tput el) # save control code for 'erase to end of line'
tput sc # save pointer to current terminal line
printf "line one - a long line\n"
printf "line two\n"
sleep 0.5
tput rc # return cursor to last cursor savepoint (`tput sc`)
printf "line three${EraseToEOL}\n" # print over `line one - a long line`; print `$EraseToEOL` to clear rest of line (in case previous line was longer than `line three`)
printf "\n" # skip over 'line two'
然后,在休眠0.5秒后line one - a long line # `tput sc` will point to this line
line two
<cursor> # cursor is left sitting on this line
将导致光标在执行最后2x tput rc
条命令之前向上移动2行:
printf
答案 1 :(得分:0)
您可以通过打印特殊的转义序列在bash脚本中移动光标,请尝试以下代码:
#!/bin/bash
# print first line
printf "first line is long\n"
# print second line
printf "line 2\n"
sleep 1
# move cursor two steps UP
printf "\033[2A"
# print line 3 (without \n)
printf "line #3"
# clear rest of first line
# and move cursor two steps down
printf "\033[K\r\033[2B"
有关ANSI转义序列的更多信息:https://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html