为什么回声“abc\r\c"?和 echo "abc\rc\c";&&sleep 1 在 bash 中表现不同? [英] why echo "abc\r\c" and echo "abc\rc\c" && sleep 1 behave differently in bash?

查看:45
本文介绍了为什么回声“abc\r\c"?和 echo "abc\rc\c";&&sleep 1 在 bash 中表现不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 bash 中运行 echo -e "abc\r\c" 时,它不输出任何内容;但是当我运行 echo -e "abc\r\c";&&sleep 1,先输出abc,一秒后清除输出.那么为什么要添加 &&sleep 1 有什么不同吗?

When I run echo -e "abc\r\c" in bash, it does not output anything; but when I run echo -e "abc\r\c" && sleep 1, it first output abc, and then clear the output after one second. So why adding a && sleep 1 make this difference?

推荐答案

第一个确实输出一些东西:字符 a、b 和 c 后跟一个回车.\c 抑制所有进一步的输出,包括 echo 通常在其参数后打印的换行符(您可以使用 echo -ne "abc\r").然后在命令退出后,shell 会打印出您的提示.由于回车和没有换行符,这会覆盖abc".如果您的提示比打印出来的要短,您会在提示后看到剩余的字符.

The first one does output something: the characters a, b and c followed by a carriage return. The \c suppresses all further output, including the newline that echo normally prints after its arguments (You can get the same effect with echo -ne "abc\r"). Then after the command exits, the shell prints out your prompt. Because of the carriage return and no newline, this overwrites the 'abc'. If your prompt is shorter than what is printed out, you'll see the leftover characters after the prompt.

对于第二个,添加 sleep 1 会导致命令退出前的延迟,让您有时间在被 shell 提示覆盖之前查看文本.

With the second one, adding the sleep 1 causes a delay before the commands exit, giving you time to see the text before it's overwritten with a shell prompt.

这篇关于为什么回声“abc\r\c"?和 echo "abc\rc\c";&&sleep 1 在 bash 中表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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