Shell – Echo command with AND operator doesn’t exit

shellzsh

echo hai && echo bye

prints

hai 
bye

while

echo hai && echo $?

prints

hai
0

When the first echo command's return value is 0, how does the echo statement after AND operator gets executed? Doesn't quick AND come out after seeing the return value 0?

Best Answer

Here, also using zsh, I have

echo hai && echo bye
hai
bye

And similarly

echo hai && echo %?
hai
0

Are you sure that you are seeing hai and bye on the same line with exactly the commands you have provided here?

In direct answer to your question, an exit status of zero is success, so the second statement is executed. (This allows different non-zero exit status values to indicate different errors.)

Related Question