Score:-3

Using the "condition ? :" construct

jp flag

I am using the following construct and cannot understand why it produces a syntax error.

(( r == 1 )) ? return 0 : return 1

What is the construct condition ? : called exactly?

muru avatar
us flag
It's usually called [the ternary operator](https://en.wikipedia.org/wiki/%3F:). Whatever makes you think you can use it in bash directly like this?
jp flag
Did not find instances of its use, so I started playing around.
Score:1
mx flag

In bash , syntax is like below:

$ echo $((0 ? 5 : 6 ))
6
$ echo $((1 ? 5 : 6 ))
5
jp flag
So the construct is simply for numeric assignment `a=$(( r==1 ? 5 : 8 ))` rather than to invoke general commands as can be done by the `if`, `case`, or `while` conditional statements.
au flag
@Fatipati Shell syntax is extremely context-dependent, and the ternary operator is no exception. It's only available in arithmetic contexts, like in `(( ))`, `$(( ))`, the index of a (non-associative) array, or a few other places.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.