2

Is there any solution in bash to this C (and some other language) "function"?

condition ? true_value : false_value

Only need a simple check: if a variable is 0, return a value else return an other value.

EDIT Thanks for answers but I want a similar:

./configure ${want_foo=1 ? --enable-foo : --disable-foo}

Is it possible or any workaround?

uzsolt
  • 5,832
  • 2
  • 20
  • 32

2 Answers2

2

The idiom I usually use is:

condition && echo yes || echo no

condition can be, for example, a test expression:

b=$( (( a == 0 )) && echo A_VALUE || echo AN_OTHER_VALUE )

Or, as in your revised question:

./configure $( (( want_foo == 1 )) && echo --enable-foo  || echo --disable-foo )

Just to be clear, you can use ?: directly in arithmetic expressions. You only need the above if you want to use strings.

rici
  • 234,347
  • 28
  • 237
  • 341
1

bash doesn't have the C style ternary operator. But you can simulate it like

[[ condition ]] && true_value || false_value

As per updated question:

./configure $( [[ want_foo == 1 ]] && echo --enable-foo || echo --disable-foo )
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • It works, but need a some edit: ${want_foo} should be in condition, and == instead of =. Thanks! – uzsolt May 28 '13 at 05:46