string=whatever
stty size | {
read y x
tput sc # save cursor position
tput cup "$((y - 1))" "$((x - ${#string}))" # position cursor
printf %s "$string"
tput rc # restore cursor.
}
Run Code Online (Sandbox Code Playgroud)
假设中的所有字符$string
都是一个单元格宽(并且$string
不包含控制字符(如换行符、制表符...))。
如果您的字符串可能包含零宽度(如组合字符)或双宽的,你可以使用ksh93的的printf
的%Ls
格式说明格式或基于字符宽度:
string='?????????'
# aka string=$'\uFF57\uFF48\uFF41\uFF54\uFF45\u0301\uFF56\uFF45\uFF52'
stty size | {
read y x
tput sc # save cursor position
tput cup "$((y - 1))" 0 # position cursor
printf "%${x}Ls" "$string"
tput rc # restore cursor.
}
Run Code Online (Sandbox Code Playgroud)
不过,这将擦除最后一行的前导部分。