简体   繁体   中英

Difference between && and || in c and unix shells?

what is the difference between these operators in C and in unix shells? In C doesn't the && mean do the first process and if it is true do the second process? How would it be different in unix shells? Thanks

edit i don't mean the difference between && and ||, but rather how they differ in c language and in unix shells.

Precedence is one big difference.

Consider the following:

a || b && c

In C , if a is true, neither b nor c need to be evaluated, since the expression is parsed as a || (b && c) a || (b && c) . However, in shell,

true || false && echo foo

will output foo , since it is parsed the same as (a || b) && c .


Also, keep in mind that some shells (like bash ) have two uses for && and || . In addition to being used to construct lists of commands as described above, they can also be used as C-style boolean operators to combine conditional expressions inside a [[ ... ]] command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM