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
880 views
in Technique[技术] by (71.8m points)

linux - What is the use of "echo || true"?

Why would anyone want to have a code like echo "something" || true ?

I found such a usage on line 92 of /lib/lsb/init-functions on an Ubuntu 14.04:

echo "$pid" || true

I understand that || stands for OR. But I could not find a way to make echo produce any sort of exit code other than zero. And even if it does, who cares?

I mean, using the prefix || true ensures that the exit code is always 0. So if the left-hand side of the double pipe yields anything different from 0, true will be executed and cause the whole line to yield 0.

Why the paranoia with the exit code of a simple echo? Is there anything checking if every single line on /lib/lsb/init-functions finishes with success?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Why the paranoia with the exit code

I believe it is to avoid script exiting due to possible use of:

set -e

at start of the script which would have caused script to exit otherwise.


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

...