0

Unable to run code, error "[[Add:" not found.


#!/bin/bash

add() {
    first="${1}"
    second="${2}"
    result=`expr $first + $second`
    echo  "The sum of two numbers are $result"  
    return $result
}


subtract() {
        first="${1}"
        second="${2}"
        result=`expr $first - $second`
        echo  "The sum of two numbers are $result"
    return $result 
}


echo "[0] Add"
echo  "[1] Subtract"

read operation

echo "Enter Number One"
read first
echo "Enter Number Two"
read second

if [["$operation" == "Add"]]
then
    add $first $second
else
    subtract $first $second
fi

I am trying to run this code, the if else comparison does not work. Each time i execute the code, at line 32 an error is occurred and else is executed. I am trying to create an addition/subtraction bash sh file.

root@Kumaraswamy:~# ./maths.sh
[0] Add
[1] Subtract
Add
Enter Number One
7
Enter Number Two
3
./maths.sh: line 32: [[Add: command not found
The sum of two numbers are 4
root@Kumaraswamy:~#
Cyrus
  • 84,225
  • 14
  • 89
  • 153

1 Answers1

1

Leave a space after [ or [[. That should work since these are real executables found in one of the directories listed under environment variable $PATH.

L_R
  • 170
  • 11
  • Actually, `[[` is a bash keyword, which allows it to have different syntax from what a regular command could have. But it's a key**word**, so (just like a command name) you can't attach something else to it. – Gordon Davisson Jun 13 '22 at 17:24