如何仅在OSX上打印回车符(\ r) [英] How to print carriage return (\r) only on OSX

查看:65
本文介绍了如何仅在OSX上打印回车符(\ r)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本(实际上是php,但概念与bash相同),可以在终端上打印进度条.我使用回车符\ r将回车符放在一行的开头.

I have a script (it's php actually, but the concept is the same as bash) that prints a progress bar in the terminal. I use carriage return \r to put the carriage back at the beginning of a line.

不幸的是,在OSX上打印\ r会产生换行符.

Unfortunately, printing \r on a OSX produces a newline.

仅在终端上的OSX上移动笔架,是否还有其他字符或简单的方法?

Is there any other character or simple way around just moving the carriage on OSX in terminal?

推荐答案

您也许可以使用 ANSI转义字符用于光标移动.

You can perhaps use ANSI escape characters for cursor movement.

printf $'\033[s'
progress=0
print_progress() { printf "%#$(($1))s" " " | tr ' ' '#' ; }
while [ $progress -lt 100 ]; do 
    print_progress $progress
    printf $'\033[u'
    sleep 0.1
    ((progress++))
done
echo

这篇关于如何仅在OSX上打印回车符(\ r)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆