-1

I am new to shell scripting

Below is my script

#!/bin/bash

first_num = 0
second_num = 0

echo -n "Enter the first number =>"
read first_num
echo -n "Enter the second number =>"
read second_num

echo "first + second = $((first_num + second_num))"

Whenever I run it, it prints like below

/Users/haani/arithmetic.bash: line 3: first_num: command not found 

/Users/haani/arithmetic.bash: line 4: second_num: command not found  

Enter the first number =>

What could be the reason for command not found here?

Mozak
  • 2,738
  • 4
  • 30
  • 49
  • 2
    Spaces aren't allowed around the = in shell variable assignment. – Jeff Bowman Dec 18 '15 at 02:45
  • 1
    Spaces. `first_num = 0` runs a command named `first_num`, passing it arguments `=` and `0`. – Charles Duffy Dec 18 '15 at 02:46
  • ...btw, `echo -n` is undefined behavior by the POSIX standard -- see http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html -- the better option is `printf`. (On XSI systems, for instance, `echo -n` prints `-n` on its output, and POSIX explicitly allows this). – Charles Duffy Dec 18 '15 at 02:47
  • I don't know what POSIX is, however I am using echo -n for making sure that the cursor stays there in the same line to take an input from the user. – Mozak Dec 18 '15 at 02:58

1 Answers1

0

try without spaces:

first_num=1
Michael WS
  • 2,450
  • 4
  • 24
  • 46