Confusion in Logical AND operation
Question
I have some confusion in how following expression works.
cc file_name.c && ./a.out
When filename.c gets compiled successfully , its exist status would be 0 (zero). So as per logical && operation , second expression ( ./a.out ) not suppose to be executed. But it still works and gives me result. How it works ?
Thanks
Solution
0
means success in the shell. It's not the same as in C. From the bash
man page:
AND and OR lists are sequences of one of more pipelines separated by the && and ││ control operators, respectively. AND and OR lists are executed with left associativity. An AND list has the form
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.
OTHER TIPS
When executing shell commands, a return value of 0 means the command was executed successfully. Non-zero value means there was an error processing the command. So, if previous command was a success it would proceed to the next command, else abort.