Tricky Dicky
Tricky Dicky

Reputation: 95

Valid assignment operators Bash

In my bash script i am trying to using the equals (=) operator as input for an IF statement, can this be done?

echo "Plese enter an operator as shown in the brackets (-)subtract, (*)Multiply, (+)Add, (/)divide, quit(=)?"
read operator
..........

if [[ $operator == '=' ]]; then 
'do something'
fi

Upvotes: 2

Views: 652

Answers (2)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58281

Try this shell.sh :

#!/bin/sh
EQUAL="=" #this is our operator
echo "Please enter operator"
read operator

if [ $operator = $EQUAL ]; then
    echo "Entered = "
else
    echo "Not = entered"
fi

Execution:

:~$ sh shell.sh
Please enter operator
=
Entered = 
:~$ sh shell.sh
Please enter operator
-
Not = entered
:~$ 

Reference to learn

Upvotes: 1

Adam Sznajder
Adam Sznajder

Reputation: 9216

I don't understand why you could not be able to do this.

> operator==
> echo $operator
=
> if [[ $operator == '=' ]]; then echo 'ok'; fi;
ok

Upvotes: 2

Related Questions