What Is the Use of "Echo || True"

What is the use of echo || true ?

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.

Why use OR TRUE operator in script with set -e?

|| is not a pipe operator. It is a shell operator meaning "or". It only executes the following command if the preceding command fails. Since true always succeeds, and otherwise does nothing, the only point of || true is to ensure that the compound command succeeds.

Normally this is unnecessary, but you can put the shell into terminate-on-failure mode with set -e. In that case, any script command which fails will cause the script to immediately terminate. (This is sometimes done in order to avoid having to check the status of every command, but it is not generally recommended as best practice.)

With set -e, it is sometimes desirable to ignore failure for certain commands (such as apt-get update); appending ||true to such a command will guarantee success and allow the script to continue even if the update fails.

How does AND and OR operators work in Bash?

From man bash

3.2.3 Lists of Commands

A list is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’, ‘&&’, or ‘||’, and optionally terminated by one of ‘;’, ‘&’, or a newline.

Of these list operators, ‘&&’ and ‘||’ have equal precedence, followed by ‘;’ and ‘&’, which have equal precedence.

So, your example

echo this || echo that && echo other

could be read like

(this || that) && other

What is the idea in this bash statement ( command || true )?

try running:(Note the bla)

which python_bla || which python3_bla_bla || true
echo $?
0

You will get RC=0. It means it a construct to successfully proceed to next command. Here we know python_bla or python3_bla_bla does not exist,but still command gave rc=0
Example: Check the RC of following three commands, I have changed the spelling of date command to incorrect but true is causing RC to remain 0.

date;echo $?
Thu Nov 9 01:40:44 CST 2017
0
datea;echo $?
If 'datea' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf datea
127
datea||true;echo $?
If 'datea' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf datea
0

Note: You can also use : operator instead of true to get the same results.Example:

command || :

Comparison operators Bash

You can just use test, without if:

x=1
y=1
[ $"{x}" -eq $"{y}" ] && echo "True" || echo "False"

Here, if x equal y, logical AND (&&) works, and script echo "True".

If x not equal, logical OR (||`) works, script echo "False".

Upd. by @randomir advice, another available solutions:

(( $"x" == $"y" )) && echo True || echo False

Or:

[[ $"a" == $"y" ]] && echo True || echo False

bash if [ false ]; returns true instead of false -- why?

You are running the [ (aka test) command with the argument "false", not running the command false. Since "false" is a non-empty string, the test command always succeeds. To actually run the command, drop the [ command.

if false; then
echo "True"
else
echo "False"
fi

What is the purpose of && :; in bash scripts

The reason it suppresses the effect of set -e can be found in the man page:

-e      Exit immediately if a simple command (see SHELL GRAMMAR above) exits with a non-zero status.
The shell does not exit if the command that fails is part of the command list immediately
following a while or until keyword, part of the test in an if statement, part of a && or ||
list, or if the command's return value is being inverted via !. A trap on ERR, if set, is
executed before the shell exits.

For emphasis: The shell does not exit if the command that fails is ... part of a && or || list

Note that there is some subtlety here. A common error is to write code like foo() { ...; rm path; #cleanup } in which the intent is to always be successful. By which I mean that the author of the code didn't really even think about the exit status of foo but implicitly expects is to succeed and doesn't care about the exit status of rm, forgetting that foo returns the exit status of rm. The code might be re-written rm path || : to ensure that foo always returns successfully, or rm path && : to return the status of rm but not exit if errexit is enabled. Frankly, it's too subtle and I believe one additional reason for never using set -e. Also, an argument could be made that you should never rely on the exit status of code unless you explicitly exit a script or return from a function.

How can I declare and use Boolean variables in a shell script?

Revised Answer (Feb 12, 2014)

the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
echo 'Be careful not to fall off!'
fi

Original Answer

Caveats: https://stackoverflow.com/a/21210966/89391

the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
echo 'Be careful not to fall off!'
fi

From: Using boolean variables in Bash

The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson's comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.



Related Topics



Leave a reply



Submit