Bash programming does not have support for ternary operator syntax.

Ternary operators are written in the Java language


The syntax is similar to the if and else conditional expression. if the expression is true, value1 is returned, else value2 is returned.

How to use ternary Operator in Bash

There are several ways we can write syntax in place of ternary operator syntax.

The first way, use if-else with expression syntax.

if [ "$AGE" -eq 25 ]; then result="true"; else result="false"; fi
echo "$result" ;

The second way, use Arithmetic expressions using && and || Syntax is

[expression1] && Expression2|| Expression3

if the expression1 is true, Expression2 is evaluated, else Expression3 is evaluated

Here is an example program

[ $AGE == 25 ] && result="true" || result="false"
echo "$result" ;

There is another way in which we can assign variables instead of expressions.

using let we can assign variables based on condition expression result

let result="(first==10)?second:third"

echo $result # true