Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Scripting Ternary operator to get string result

In shell scripting, I am using ternary operator like this:

(( numVar == numVal ? (resVar=1) : (resVar=0) ))

I watched shell scripting tutorial by Derek Banas and got the above syntax at 41:00 of the video

https://www.youtube.com/watch?v=hwrnmQumtPw&t=73s

The above code works when we assign numbers to resVar, but if I try to assign a string to resVar, it always returns 0.

(( numVar == numVal ? (resVar="Yop") : (resVar="Nop") ))

and also tried

resVar=$(( numVar == numVal ? (echo "Yop") : (echo "Nop") ))

So which is the right way to do this?

like image 804
Sujay U N Avatar asked Dec 31 '17 10:12

Sujay U N


People also ask

Can we use ternary operator in shell script?

Bash - Ternary OperatorBash programming does not have support for ternary operator syntax. The syntax is similar to if and else conditional expression. if expression is true, value1 is returned,else value2 is returned.

What is $$ in shell script?

$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing. 8.

What does && do in shell script?

Logical AND operator(&&):The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.

What is $_ in shell?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


2 Answers

You didn't tell us what shell you use but it's possible you use bash or something similar. Ternary operator in Bash works only with numbers as explained in man bash under ARITHMETIC EVALUATION section:

The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for over- flow, though division by 0 is trapped and flagged as an error. The operators and their precedence, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
(...)

expr?expr:expr

conditional operator

And the reason that resVar is assigned 0 when you use "Yop" or "Nop" is because such string is not a valid number in bash and therefore it's evaluated to 0. It's also explained in man bash in the same paragraph:

A null value evaluates to 0.

It's also explained in this Wikipedia article if you find it easier to read:

A true ternary operator only exists for arithmetic expressions:

((result = condition ? value_if_true : value_if_false))

For strings there only exist workarounds, like e.g.:

result=$([ "$a" == "$b" ] && echo "value_if_true" || echo "value_if_false")

(where "$a" == "$b" can be any condition test, respective [, can evaluate.)

like image 116
Arkadiusz Drabczyk Avatar answered Nov 15 '22 09:11

Arkadiusz Drabczyk


Arkadiusz already pointed out that ternary operators are an arithmetic feature in bash, not usable in strings. If you want this kind of functionality in strings, you can always use arrays:

$ arr=(Nop Yop)
$ declare -p arr
declare -a arr='([0]="Nop" [1]="Yop")'
$ numVar=5; numVal=5; resvar="${arr[$((numVar == numVal ? 1 : 0))]}"; echo "$resvar"
Yop
$ numVar=2; numVal=5; resvar="${arr[$((numVar == numVal ? 1 : 0))]}"; echo "$resvar"
Nop

Of course, if you're just dealing with two values that can be in position 0 and 1 in your array, you don't need the ternary; the following achieves the same thing:

$ resvar="${arr[$((numVar==numVal))]}"
like image 44
ghoti Avatar answered Nov 15 '22 09:11

ghoti