Home > Back-end >  Bash Can the ternary operator be used to set values to multiple variables?
Bash Can the ternary operator be used to set values to multiple variables?

Time:09-23

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?

CodePudding user response:

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 ))
  • Related