Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

I saw the following bash statement being used on the internet:

PYTHON_BIN_PATH=$(which python || which python3 || true)

I understand that if which python fails, then which python3 will be executed, but I don't understand the purpose of the true at the end of the condition. Any idea?

like image 309
Elad Weiss Avatar asked Oct 29 '22 23:10

Elad Weiss


1 Answers

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 || :
like image 61
P.... Avatar answered Nov 15 '22 12:11

P....