bash 在同一行继续输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3372673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:32:07  来源:igfitidea点击:

Continue output on same line

bash

提问by Johan

Want to do a fancy effect by printing [OK] at the end of the last line when a task is finished.

想通过在任务完成时在最后一行的末尾打印[OK]来做一个花哨的效果。

Got this:

明白啦:

echo "
echo -n "
printf "%s" "##代码##: Starting backup process in '$backupdir'... "
sleep 3
printf '[OK]\n'
: Starting backup process in '$backupdir'... "
: Starting backup process in '$backupdir'... " sleep 3 echo '[OK]'

I tried different things at the end of the first echo, at the beginning of the second like \r, \c, \, Googled it... No good.

我在第一个回声结束时尝试了不同的事情,在第二个回声开始时尝试了不同的东西,例如\r, \c, \, 谷歌搜索...不好。

I want it to output this:

我希望它输出这个:

./backup.sh: Starting backup process in '/backup'...

./backup.sh: Starting backup process in '/backup'...

... And 3 sec later, add the [OK]:

... 3 秒后,添加[OK]

./backup.sh: Starting backup process in '/backup'... [OK]

./backup.sh: Starting backup process in '/backup'... [OK]

Thanks,

谢谢,

回答by Johan

Use the -noption to skip printing newline at the end. More reference at echodocs.

使用-n选项在末尾跳过打印换行符。更多参考echo文档

So you could do something like this:

所以你可以做这样的事情:

##代码##

回答by ghostdog74

use the more portable printf:

使用更便携printf

##代码##

回答by ULysses

echo -n hello

echo -n hello