4
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?

1
  • 3
    0 means true. && means : execute the next instructions if and ony if the preceding one was true. therefore what you see is normal. Apr 20, 2015 at 0:28

3 Answers 3

23

Your confusion stems from the fact that many popular languages (especially C-based ones) stop evaluating && sequences when 0 is encountered, because 0 is considered false and everything else is true. In Bash, however, that's not the case. By convention, in POSIX systems (and all other Unix-like systems), return code 0 is considered SUCCESS (there was no error, so nothing is returned) and a non-zero return code is considered FAILURE. Every command in Bash, be it an external program such as a C program or a shell builtin, must return a value:

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

The return value of a simple command is its exit status, or 128+n if the command is terminated by signal n.

(...)

Shell builtin commands return a status of 0 (true) if successful, and non-zero (false) if an error occurs while they execute. All builtins return an exit status of 2 to indicate incorrect usage.

A return value is not a Boolean, though. It's a number between 0 and 255:

The exit status of an executed command is the value returned by the waitpid system call or equivalent function. Exit statuses fall between 0 and 255, though, as explained below, the shell may use values above 125 specially. Exit statuses from shell builtins and compound commands are also limited to this range. Under certain circumstances, the shell will use special values to indicate specific failure modes.

For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal N, bash uses the value of 128+N as the exit status.

(Bolding mine.)

When a command reports its return code back to the shell, it's generally enough to check whether the exit code is 0 or not.

Now, the next command in a list glued together with && will be executed only if the previous command returned 0—i.e. SUCCESS:

AND and OR lists are sequences of one or 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.

7

The return value from commands are different from typical boolan values. 0 is success when executing a command, anything else is failure. && expects 0 to me success here for that reason.

4

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.)

4
  • does hai and bye come on same line? that is my mistake....
    – user93868
    Apr 19, 2015 at 15:40
  • @MadhavanKumar also for hai and 0?
    – roaima
    Apr 19, 2015 at 15:43
  • They will be on different lines, unless you use echo -n to suppress the newline. Perhap something s wrong with your terminal?
    – mattdm
    Apr 19, 2015 at 15:45
  • @roaima changed...
    – user93868
    Apr 19, 2015 at 15:46

You must log in to answer this question.