Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Are the following two command lists portably equivalent?

$foo; echo $? #(1)


$foo && echo 0 || echo $? #(2)


Consider the command exit-status defined so:

exit "$1"

Does the POSIX standard forbid the following?

> exit-status 2 && echo "impossible branch"; echo $?

If not, then a POSIX-compliant shell could fail to equate the two uppermost command lists. Which isn't quite the same thing as not being portable...

share|improve this question
Since the command lines do not contain a | symbol they are not pipelines. – maxschlepzig Dec 16 '11 at 18:53
@Max: That's actually right. I've called lists of commands/pipelines separated by && or || 'pipelines' for ages, though. Do they have a proper name? – Charles Stewart Dec 16 '11 at 20:11
They are called 'command lists' - or if one only includes logical operators, you can call it 'logical expression' - see for example bash(1) – maxschlepzig Dec 16 '11 at 20:50
@maxschlepzig As long as you're quibbling, this is a pipeline that happens to have a single component. – Gilles Dec 17 '11 at 23:09
up vote 3 down vote accepted

Both command lines are portable. And both have the same effect.

Regarding the 2nd: In case $foo fails, echo 0 is not evaluated, thus echo $? prints the exit status of the last command - i.e. $foo.

The logical operators are left-associative, i.e. the 2nd command line is equivalent to:

$ { $foo && echo 0 } || echo $? 

(where the first $ denotes the shell prompt)

Shell Command Language standardizes command lists, operator associativity and so on.

Section 2.9.3 of Shell Command Language also specifies the exit status semantics of the && operator:

Exit Status

The exit status of an AND list shall be the exit status of the last command that is executed in the list.

Regarding your postscript: Yes, POSIX does not allow it.

share|improve this answer
This says what I think, but it doesn't help me see why it must be true. Does it follow from the POSIX specs, or is there some shell semantics that can be applied to see this? – Charles Stewart Dec 16 '11 at 20:14
Yes, see Shell Command Language, especially 2.9.3, which includes a 'AND Lists' paragraph and a following 'Exit Status' section. – maxschlepzig Dec 16 '11 at 20:57
I don't see anything in section 2.9 that says what the exit status of cmd1 && cmd2 is when cmd1 has an exit status other than 0 or 1. I'll add a bit to my question to expand on this. – Charles Stewart Dec 16 '11 at 21:13
@CharlesStewart, take a closer look - I updated the answer which now includes a direct quote from the referenced paragraph. – maxschlepzig Dec 16 '11 at 21:27
Sorry for being obtuse. I was confused about command lists and completely misread that specification. +1, clear and correct. – Charles Stewart Dec 17 '11 at 13:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.