문제

Is there any functional advantage of using a simple if-then statement such as

if [ … ]; then
do

over using short-circuit list evaluators with command groups such as

[ … ] && {
}

Of course, if-then is the standard way of writing it, but is there any actual difference/advantage from a functional point of view?

Using () instead of {} would spawn a new subshell, thus creating a new scope for variables inside the if-then block.

도움이 되었습니까?

해결책

Not with your example, but the commonly seen

condition && foo || bar

is not the same as

if condition
then
  foo
else
  bar
fi

In the former case, bar will run not only if condition fails, but if foo fails.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top