0

running - ./bashfile.sh a 1 1

#!/bin/bash

addit () {
echo $(($2 + $3))
}

if [ $1 == a ]
then
addit
fi

produces

syntax error: operand expected (error token is "+ ")

What is causing this problem?

Thanks

4
  • You should call your addit function in the script, something like: addit $1 $2, after the definition of addit. Commented Oct 19, 2014 at 4:40
  • You are not calling the function... Commented Oct 19, 2014 at 4:47
  • In my haste I neglected that the function is called inside of an if statement. I have added it now, sorry. Commented Oct 19, 2014 at 5:17
  • The parameters that addit see are those you pass to the function, not those that are passed to the script. Call addit with $2 and $3 as I show you in my answer. Commented Oct 19, 2014 at 5:24

1 Answer 1

2

You should call your addit function in the script, something like: addit $1 $2, after the definition of addit.

#!/bin/bash
addit () {
    echo $(($1 + $2))
}

addit $1 $2

Running:

chmod +x bashfile.sh
./bashfile 1 1
2
Sign up to request clarification or add additional context in comments.

Comments

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.