What is the use of “echo || true”?

孤街浪徒 提交于 2019-12-30 06:16:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/28773880/what-is-the-use-of-echo-true

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!