[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] The difference between 'if cond; then' and 'cond && {'
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] The difference between 'if cond; then' and 'cond && {' |
Date: |
Mon, 3 Dec 2018 16:27:57 -0700 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
Peng Yu wrote:
> > > It sounds like the following construct would always produce the same
> > > result. Is it so?
> > >
> > > if some_cond; then
> > > do something1
> > > do something2
> > > fi
> > >
> > > somc_cond && {
> > > do something1
> > > do something2
> > > }
> >
> > There is a difference if you want to
> Could you give an example when they can not be made to return the same
> result no matter what? Thanks.
There are interactions with 'set -e'. But "as everyone who is anyone
knows" (proof by intimidation) one should never use 'set -e'. :-)
If you need to ask then you shouldn't be doing it.
> > - use "else"
>
> I assume there is no "else".
Precedence in the shell is strictly left to right for && and ||.
Therefore one can say:
some_cond && {
do something1
do something2
} || {
do something3
do something4
}
$ true && echo yes || echo no
yes
$ false && echo yes || echo no
no
But anything more complicated than a one-liner and I strongly
recommend the 'if...then...else...fi' construct to be better. And the
rest of the precedence rules can cause all kinds of confusion when
used with pipes and other constructs. Don't do it!
Bob