puwlah
puwlah

Reputation: 113

Bash Can the ternary operator be used to set values to multiple variables?

the (pseudo) code for what i want to do:

n=100
desired=""
for i in {1..10} ; do
    (( $(numeric_command_output) < n ?
        set n to the output and desired to $i :
        keep n and desired unchanged )) ; done

I know how to manipulate one variable based on the condition using ternary operator, but can this be done in bash?

Upvotes: 0

Views: 428

Answers (1)

KamilCuk
KamilCuk

Reputation: 141483

Can the ternary operator be used to set values to multiple variables?

Yes.

but can this be done in bash?

Yes.

(( ( tmp=$(numeric_command_output) ) < n ? (n=tmp, desired=$i) : 0 ))

or

(( tmp=$(numeric_command_output, tmp < n ? (n=tmp, desired=$i) : 0 ))

Upvotes: 3

Related Questions