Evaluating false in bash with multiple commands without a subshell

1

Bash evaluates the logical operators such that && always takes precedence. So for example:

false || echo 1 && echo 2
1
2

and

true || echo 1 && echo 2
2

Ok. So lets say I want the output to be like this:

false || (echo 1 && echo 2)

But without invoking a subshell.

The only solution I could think of is this:

false || if true; then echo 1; echo 2; fi
1
2

Is there any cleaner way, similar to parenthesis in C, to group commands together without having to invoke a subshell?

Zhro

Posted 2014-10-28T10:52:16.503

Reputation: 471

In retrospect, this should probably have been posted on stackoverflow. – Zhro – 2014-10-28T10:53:11.190

No, it's also fine to ask here. – slhck – 2014-10-28T11:22:33.477

Answers

4

false || { echo 1 && echo 2; }

Cyrus

Posted 2014-10-28T10:52:16.503

Reputation: 4 356

This seems to be accurate. No parallel processing subshell is created when I do this: { sleep 0.2s; echo 1; } || { echo 1 && echo 2; } – Zhro – 2014-10-28T18:32:51.693