Linux command operators: &&, ||, and \

Jan
1 min readMay 13, 2020

--

A quick glimpse:

  • A ; B : Run A and then B, regardless of the success of A
  • A && B : Run B if A succeeded
  • A || B : Run B if A failed

&&

It is often to see commands are written in one line, such as

$ command A  &&  command B

The intent is to execute command B after command A only if command Ais executed successfully. In this way, commands are chained together, such that the next command is run if and only if the preceding command exited without errors.

||

on the contrary, If two commands are chained by a ||, such as

$ command A  || command B

It means command B will be executed only if Command A doesn't runs successfully (exit status is false).

;

; is just a separator which doesn't care what happened to the command before.

command A ; 
command B

\

\ is used at the end of a line, as a means of concatenating lines together and run as a single command. So the following two commands are processed exactly the same.

this   \
is command A

is the same as

this is command A

References:

bash — Confusing use of && and || operators — Unix & Linux Stack Exchange https://unix.stackexchange.com/questions/24684/confusing-use-of-and-operators

--

--

Jan
Jan

Written by Jan

Win is every step of the process.

No responses yet