help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Conditional operator ?: in manual


From: Greg Wooledge
Subject: Re: Conditional operator ?: in manual
Date: Sat, 18 Mar 2023 11:15:39 -0400

On Sat, Mar 18, 2023 at 02:42:38PM +0000, uzibalqa wrote:
> I am trying to find a description about the conditional operator ?:  (e.g. 
> expr ? expr : expr ), but it is difficult to locate in the manual.  
> 
> It would be good to have it included with "3.2.5.2 Conditional Constructs".

It works like the ternary ?: operator in C and related languages.
The first expression is evaluated for "trueness" (nonzero is true, zero
is false).  If the first expression's value is nonzero, then the result
is the evaluation of the second expression.  If the first expression's
value is zero, then the result is the evaluation of the third expression.

Since this operator only appears in math contexts in bash, it's
restricted to integer results.  So you CANNOT do something like this:

currency=$(( country == "US" ? "dollar" : "euro" ))

No strings allowed.  Only integers.  This makes it mostly useless in
real scripts, so there's no overwhelming urgency to expand the
documentation.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]