首页 > 解决方案 > bash:用源加载的变量不能用于算术运算

问题描述

我有一个带有变量的简单文件(test.ini):

v2="1"

在脚本中,我尝试对加载的变量进行算术运算,但总是得到一个“无效的算术运算符”。

脚本:

#!/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))"

我在 Centos 7 和 bash 4.2.46(2)-release 上运行它。

我搜索了谷歌,找不到类似的东西。非常感谢任何帮助。

沃尔德马尔

标签: bash

解决方案


推荐阅读