Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

shell

sh

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?

like image 333
Akira Avatar asked Feb 27 '15 20:02

Akira


People also ask

For what purpose echo is used?

An echocardiogram, or "echo", is a scan used to look at the heart and nearby blood vessels. It's a type of ultrasound scan, which means a small probe is used to send out high-frequency sound waves that create echoes when they bounce off different parts of the body.

What does true do in Linux?

On Unix-like operating systems, the true command's sole purpose is to return a successful exit status. It is useful when you want part of a command or conditional expression always to be true.

What is == in shell?

'==' Operator: Double equal to operator compares the two operands. Its returns true is they are equal otherwise returns false. '! =' Operator: Not Equal to operator return true if the two operands are not equal otherwise it returns false.


1 Answers

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.

like image 96
anubhava Avatar answered Sep 21 '22 21:09

anubhava