Shell logic AND, cmd1 && cmd2 is the other way around, right?

We know what &&

boolean means AND

, so:

true && true  => true
false && true => false


When we work in Shell (Bash here), a successful command call returns 0

. Has the shell changed 0

to non-zero

before AND

? Or will Shell just change normal logics


As an example:

cat file1 && cat file2



will be cat-ed

, only if it file1

can be cat-ed



source to share

2 answers

In Bash, boolean true

is represented as 0

, while boolean false

is represented as non-zero. This allows the output value of the command to be used in a logical operation.

You can find out more by looking at the documentation on bash statements .

One common idiom is to chain commands using &&

, so that if any command in the chain fails, the following commands are not executed:

cmd1 && cmd2 && cmd3




In shell (bundled bash) &&

means boolean AND. Parameters &&

can be commands that will be evaluated according to their return value - a return value of 0 indicates success (true), other values ​​fail (false).

So in the C / C ++ sense it's the opposite (in C / C ++ and its ilk 0 = false, other = true), but from the shell's point of view it's not (success = true, fail = false)

UPDATE: changed explanation based on comment, which &&

does not necessarily involve executing commands



All Articles