Did I Execute?
I’ll be discussing about a small detail, about executing multiple commands on a terminal, which I realised most people don’t know about.
There are three ways in which commands are combined:-
What people don’t realise is that all of them are meant for completely different use cases.
Let me elaborate.
the legit command separator. Let us see some examples.
$ false ; echo "OK" OK $ true ; echo "OK" OK
A ; B implies Run A and then B. Very straight-foward. No tricks. Lets move to
&& and Double pipe
|| are Logical Binary Operators and as you might have guessed
&& is Logical AND,
|| is Logical OR.
The operands they take are boolean values -
But where are the boolean values in
echo hello && echo world! ?
These values are derived from the Exit status of the commands!
Note that a logical AND checks the second operand only when the first one is
true. Similarly logical OR checks the second operand only when the first one is
A && B, B executes only when A is a success( exit status 0 ) while in
A || B, B executes only when A returns an error. Thus both commands may not always execute! Lets see some examples.
$ false && echo "OK" $ true && echo "OK" OK $ false || echo "OK" OK $ true || echo "OK" $
This is unlike
A ; B where B always executes irrespective of exit status of A. Both methods have their uses. A simple case where I use this little detail is while compiling programs.
g++ -o hack hack.cpp && ./hack
Here the binary is not executed if the compilation is not successful.
test_command && echo success || echo fail
We can get the proper control flow depending on whether
test_command was a success or not.
$ true && echo success || echo fail success $ false && echo success || echo fail fail
Hope this helped :) In case you have any doubts comment below. If you are an Infosec person, don’t forget to checkout my Write-ups
Awesome Terminal Commands