Skip to main content
added 459 characters in body
Source Link
Hauke Laging
  • 94.5k
  • 21
  • 132
  • 185

if testlist; then else ...fi and testlist && thenlist || elselist are not equivalent in general. The difference is that in the if construct exactly one of thenlist and elselist is executed. In the other case it depends on the exit code of thenlist (and possible eliflist blocks).

Thus you have to add (e.g.) true at the end of a selected result list so that the list has the same exit code like the test list that caused its execution.

if true; then
  echo true
  test -e doesntexist
else
  echo false
fi

can be rewritten with && and || by making sure the exit code of the code blocks. Instead of

true && { echo true; test -e doesntexist; } || echo false

you need

true && { echo true; test -e doesntexist; true; } || echo false

elif

if test -e exists; then
  echo true
elif test -e doesntexist
  echo elif
else
  echo false
fi

would be

test -e exists && { echo true; true; } ||
  { test -e doesntexist && { echo elif; true; } || echo false; }
    
if true; then
  echo true
  test -e doesntexist
else
  echo false
fi

can be rewritten with && and || by making sure the exit code of the code blocks. Instead of

true && { echo true; test -e doesntexist; } || echo false

you need

true && { echo true; test -e doesntexist; true; } || echo false

elif

if test -e exists; then
  echo true
elif test -e doesntexist
  echo elif
else
  echo false
fi

would be

test -e exists && { echo true; true; } ||
  { test -e doesntexist && { echo elif; true; } || echo false; }
    

if testlist; then else ...fi and testlist && thenlist || elselist are not equivalent in general. The difference is that in the if construct exactly one of thenlist and elselist is executed. In the other case it depends on the exit code of thenlist (and possible eliflist blocks).

Thus you have to add (e.g.) true at the end of a selected result list so that the list has the same exit code like the test list that caused its execution.

if true; then
  echo true
  test -e doesntexist
else
  echo false
fi

can be rewritten with && and || by making sure the exit code of the code blocks. Instead of

true && { echo true; test -e doesntexist; } || echo false

you need

true && { echo true; test -e doesntexist; true; } || echo false

elif

if test -e exists; then
  echo true
elif test -e doesntexist
  echo elif
else
  echo false
fi

would be

test -e exists && { echo true; true; } ||
  { test -e doesntexist && { echo elif; true; } || echo false; }
    
Source Link
Hauke Laging
  • 94.5k
  • 21
  • 132
  • 185

if true; then
  echo true
  test -e doesntexist
else
  echo false
fi

can be rewritten with && and || by making sure the exit code of the code blocks. Instead of

true && { echo true; test -e doesntexist; } || echo false

you need

true && { echo true; test -e doesntexist; true; } || echo false

elif

if test -e exists; then
  echo true
elif test -e doesntexist
  echo elif
else
  echo false
fi

would be

test -e exists && { echo true; true; } ||
  { test -e doesntexist && { echo elif; true; } || echo false; }