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,450
  • 2
  • 18
  • 31

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
  • 219,119
  • 25
  • 219
  • 314
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
  • 71,025
  • 22
  • 98
  • 142