fcm fcm - 1 year ago 69
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' }


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


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


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

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download