0

I'm trying to write a bash script that displays temperatures in Celsius and then converts and displays it to Fahrenheit. I keep getting an operand error at the line of the Fahrenheit equation. Is it a little thing that I'm missing?

#!/bin/bash

cels = 0
fahr = 0

echo -e "Celsius\t\tFahrenhiet"
echo "--------------------------"

while [[ $cels -le 25 ]]
do
  fahr =$(( ($cels) * 9/5 + 32)) <---operand expected error here
    echo -e $cels "\t\t" $fahr
    ((cels++))
done

Thanks for any help in advance, I'm new to bash and am having a lot more trouble with it than other languages.

  • 1
    https://www.shellcheck.net/ – Sean Bright Nov 20 '19 at 22:29
  • 1
    Welcome to SO! Bash is weird when it comes to whitespace, and this is due to having spaces around the `=` (you can't have space before or after it). You undoubtedly also got some `cels: command not found` type errors before the current one, so I'm marking it as a duplicate of that since it does a good job of explaining the problem – that other guy Nov 21 '19 at 01:03

1 Answers1

0

Nevermind I was able to find an error in my equation.

Changing my equation to fahr=$(((9/5) * $cels + 32)) fixed my program

  • This won't get the right answer -- try it with `cels=100`, and it'll give 132 instead of 212 (the correct answer). This is because bash doesn't do floating point math, just integer math, so `9/5` will get rounded down to just 1. `$(((9 * cels) / 5 + 32))` would work better, but wouldn't give exact answers for `cels` values that aren't divisible by five, and will fail entirely if `cels` has a decimal point. It's really better to use a different program for math. – Gordon Davisson Nov 21 '19 at 01:47