Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

shell - why echo "abc c" and echo "abc cc" && sleep 1 behave differently in bash?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "). 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...