如何在UNIX中将字符串转换为整数
问题描述:
我有d1="11"
和d2="07"
.我想将d1
和d2
转换为整数并执行d1-d2
.如何在UNIX中做到这一点?
I have d1="11"
and d2="07"
. I want to convert d1
and d2
to integers and perform d1-d2
. How do I do this in UNIX?
d1 - d2
当前为我返回"11-07"
作为结果.
d1 - d2
currently returns "11-07"
as result for me.
答
标准解决方案:
expr $d1 - $d2
您也可以这样做:
echo $(( d1 - d2 ))
,但是请注意,这会将07
视为八进制数字! (因此07
与7
相同,但010
与10
不同).
but beware that this will treat 07
as an octal number! (so 07
is the same as 7
, but 010
is different than 10
).