0

I have a simple file (test.ini) with a variable:

v2="1"

And in script I try to do arithmetic operation on the loaded variable and I always get an 'invalid arithmetic operator'.

Script:

#!/bin/bash

# Works as it should.
v1="1"
echo "$((v1+1))"

# Throws ")syntax error: invalid arithmetic operator (error token is ".
source "test.ini"
echo "$((v2+1))"

I'm running this on Centos 7 and bash 4.2.46(2)-release.

I searched around the Google and couldn't find anything similar. Any help is really appreciated.

Br Waldemar

Waldemar
  • 171
  • 9
  • I tried to reproduce your problem with `source <(echo 'v2="1"'); echo "$((v2+1))"` but got no error. It seems like either `v2` is not `1` but contains a `.` or there is another arithmetic expression in your `test.ini` which causes the error. Please give us a *reproducible* example. – Socowi Jan 16 '20 at 12:59
  • 2
    Almost certainly the *actual* problem is not that you *source* the file, but that the file contains DOS line endings. – tripleee Jan 16 '20 at 13:01
  • To confirm tripleee's guess please run `source <(tr -d \\r < test.ini)` instead of `source test.ini` and see if it works. – Socowi Jan 16 '20 at 13:02
  • Thanks guys. That was the issue yes. Totally forgot about switching to unix line endings. :/ – Waldemar Jan 16 '20 at 14:04

0 Answers0