0

in C I can assign a value in this way:

val = (cond)? val_true : val_false;

and it's equivalent to

if (cond) 
    val = val_true;
else
    val = val_false;

Are there an equivalent of val=(cond)? val_true : val_false in shell bash?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Is it slightly different than the one with which it is being compared? Its a bit confusing for a newbie (although not for someone with 10.4K rep) –  Jan 02 '14 at 13:35

1 Answers1

1

Suppose you want it to be somewhat like this:

true=1
[ $true ] && p=1 || p=0

All you have to do is keep whatever condition you want within those brackets.

If it’s valid, then the branch after the AND is followed, otherwise that after the OR is followed.

This is equivalent to this:

if [ $true ]; then p=1; else p=0; fi

So you should be aware of the construct in case you ever run into it, but it’s arguably less readable than just listing out explicitly what you’re doing.