What is the difference between | and || ?
When I run this command :
ls -la | id
I get the result of id
When I run this command
:
ls -la || id
I get the result of ls -la
So what is the difference between them ?
bashcommand line
What is the difference between | and || ?
When I run this command :
ls -la | id
I get the result of id
When I run this command
:
ls -la || id
I get the result of ls -la
So what is the difference between them ?
Best Answer
|
is the pipe operator, which passes the output of the first command to the one that follows.from
man bash
:In the example you provide,
id
doesn't seem to do anything with the output ofls
so it just returns the same output as runningid
alone.||
is the logical OR operator, and specifies what to do if the first command returnsfalse
or fails (is non-zero).from
man bash
:In your example,
ls -la
runs successfully so theid
command isn't run. If you did the following:and try to pass an invalid option
z
tols
, then it fails and theid
command gets run.