1

I am trying to learn some unix and I am trying to script some basic variables, any idea why this won't work when I sh math?

> vi math 
A=5
B=10
let C=A+B
let D=B-A
let E=A*B
let F=A/B
echo $A $B $C $D $E $F

here is what is showing.

math: 4: math: let: not found
math: 5: math: let: not found
math: 6: math: let: not found
math: 7: math: let: not found
5 10
6
  • What is not working? You should use bash to run it using bash ./math Commented Mar 25, 2014 at 14:16
  • The modern POSIX-standard way to do math is C=$((A+B)). let is a relic of a bygone age. Commented Mar 25, 2014 at 14:35
  • ...also, "won't work" isn't exactly useful. If you described exactly what it does, that would let folks be more helpful. Commented Mar 25, 2014 at 14:36
  • Your output is identical to what I see when using dash. Can you show the output of: bash math and then sh math and then dash math? I expect the output of sh math to be identical to that of dash math. Commented Mar 27, 2014 at 17:59
  • Yeah, it works when I do Bash math... > sh math math: 4: math: let: not found math: 5: math: let: not found math: 6: math: let: not found math: 7: math: let: not found 5 10 > bash math 5 10 15 5 50 0 > dash math math: 4: math: let: not found math: 5: math: let: not found math: 6: math: let: not found math: 7: math: let: not found 5 10 Commented Mar 27, 2014 at 18:17

1 Answer 1

5

On a system where /bin/sh is Bash (Mac OS X 10.9.2 Mavericks), then I get:

$ cat math
A=5
B=10
let C=A+B
let D=B-A
let E=A*B
let F=A/B
echo $A $B $C $D $E $F
$ sh math
5 10 15 5 50 0
$ bash math
5 10 15 5 50 0
$ ksh math
5 10 15 5 50 0
$ dash math
math: 3: math: let: not found
math: 4: math: let: not found
math: 5: math: let: not found
math: 6: math: let: not found
5 10
$ hsh math
math: let: not found
math: let: not found
math: let: not found
math: let: not found
5 10
$

(hsh is the Heirloom Bourne Shell installed as another name.)

That means that both sh math and bash math do the arithmetic as you intended. Are you perhaps on a machine with a different shell as /bin/sh (perhaps it is dash in disguise and you're on Ubuntu)?

Sign up to request clarification or add additional context in comments.

3 Comments

Doing this #!/bin/sh A=5 B=10 let C=A+B let D=B-A let E=A*B let F=A/B echo $A $B $C $D $E $F but it i still not working.
Can you provide more details about your environment? Which O/S and version? Which version of Bash? (Use bash --version to find out. Does /bin/sh --version say the same thing?) When you say "it is not working", what exactly are you seeing as error messages? Please add these details to the question rather than the comments here. And in future, please include in particular the exact error messages you are seeing in the question up front; it means we have to make fewer guesses about what you're seeing go wrong.
GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)editing my question with what appears.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.