Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary operator (?:) in Bash

Is there a way to do something like this

int a = (b == 5) ? c : d; 

using Bash?

like image 827
En_t8 Avatar asked Oct 17 '10 14:10

En_t8


People also ask

Does bash have a ternary operator?

Bash programming does not have support for ternary operator syntax. The syntax is similar to if and else conditional expression.

How do you write a 3 condition ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What does =~ mean in bash?

A regular expression matching sign, the =~ operator, is used to identify regular expressions. Perl has a similar operator for regular expression corresponding, which stimulated this operator.

What is ternary operator with example?

A ternary operator lets you assign one value to the variable if the condition is true, and another value if the condition is false. The if else block example from above could now be written as shown in the example below. var num = 4, msg = ""; msg = (num === 4) ?


1 Answers

ternary operator ? : is just short form of if/else

case "$b" in  5) a=$c ;;  *) a=$d ;; esac 

Or

 [[ $b = 5 ]] && a="$c" || a="$d" 
like image 85
ghostdog74 Avatar answered Oct 08 '22 12:10

ghostdog74