When I run the following bash script:

Success: $(grep success * | grep B2B | wc -l)
etc_error: $(grep etc_error * | grep B2B | wc -l)
map_timeout: $(grep map_timeout * | grep B2B | wc -l)

success=$(grep success * | grep B2B | wc -l)
etc=$(grep etc_error * | grep B2B | wc -l)
map=$(grep map_timeout * | grep B2B | wc -l)

sum=$(($success + $etc + $map))
total=$(($success / $sum))
echo $total

I got the error:

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

What is wrong with my script?

closed as unclear what you're asking by G-Man, Michael Homer, X Tian, Rui F Ribeiro, Kiwy May 20 at 11:45

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • The first three lines ought to give you Command not found errors in bash, unless you have scripts called Success:, etc_error: and map_timeout: in your PATH. – Kusalananda Nov 14 '18 at 7:54
  • 2
    Either you're not showing the full script or you're not showing the full error messages. Try also running with bash -x to see what happens. – Stéphane Chazelas Nov 14 '18 at 9:00

This doesn’t explain everything — given your variable declarations, they should have a value — but since you’re using arithmetic expansion, you don’t need $ to introduce variables; undefined variables then default to 0:

sum=$((success + etc + map))
total=$((success / sum))
echo "$total"

Ideally you should check that sum is non-zero before dividing, but at least you’ll get a sensible error message with this approach even if you don’t check sum (“division by 0” rather than “syntax error: operand expected”, in bash).

The error you’re getting comes from

sum=$(($success + $etc + $map))

For some reason, success, etc and map are all undefined or empty. The shell expands the above to

sum=$(( + + ))

and then tries to expand that, which results in the syntax error you quote.

This is rather surprising though since

success=$(grep success * | grep B2B | wc -l)

should produce a value...

BTW, You may want to improve the above to:

success=$(grep success ./* | grep -c B2B)

or, if B2B is to be matched on the content of the success lines and not on the file names:

success=$(cat ./* | grep success | grep -c B2B)

or with GNU grep or compatible:

success=$(grep -h success ./* | grep -c B2B)
  • of course if you default to zero mathematics forbid dividing by zero :) – francois P Nov 14 '18 at 7:50

you have to check your variables before the addition, because if one of then is not set or has no numeric value the addition syntax will fail

you can defaulty set vars to 0 before setting then to avoid that.

  • What do you mean check variables before addition? Can you give some examples? Thanks! – Cyril Nov 14 '18 at 7:43
  • @Cyril Basic debugging: Output the values of the variables so that you can inspect them and see whether they are what you expect them to be. – Kusalananda Nov 14 '18 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.