2013-08-27

Ternary operator(?:) in bash

Ternary operator is a short form of if/then/else
case $a in
    1) b=$c ;;
    *) b=$d ;;
esac

[[ $a=1 ]] && b=$c || b=$d
# At this case, 3rd part can be executed in case 2nd part fails

[[ $a=1 ]] && { b=$c; true; } || b=$d
# to avoid executing 3rd part by accident when 2nd part fails
## semicolon(;) is needed in {}

No comments:

Post a Comment