fcm fcm - 3 months ago 9
Linux Question

linux curly brackets instead of if-then-else-fi do-done

Are curly brackets a suitable replacement for other constructs?

[ -n "$TEST" ] && { XX='yes'; echo 'yes' } || { XX='no'; echo 'no' }


vs

if [ -n "$TEST" ] ; then
XX='yes'; echo 'yes'
else
XX='no'; echo 'no'
fi


another:

for num in {000..2}; do echo "$num"; done


or

for num in {000..2}; { echo "$num"; }

Answer

In the your first example, A && B || C and if A; then B else C have different behavior if B has a non-zero exit status.

[ -n "$TEST" ] && { echo this; false; } || { echo "this too?"; }

Your second example is should be a syntax error. Nothing in the documentation says you can replace the do/done pair with another compound command (which is what {...} is).

Comments