derrend
derrend

Reputation: 4636

Bash function behaves differently in script than when entered manually on command line, why?

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

Upvotes: 0

Views: 227

Answers (1)

rendon
rendon

Reputation: 2363

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

Upvotes: 2

Related Questions