You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用expr命令计算整数加法时遭遇non-integer argument错误的解决咨询

使用expr命令计算整数加法时遭遇non-integer argument错误的解决咨询

Hey there! Let's work through why your expr command is throwing that non-integer argument error—this is a super common gotcha with the expr tool, so I’ve got a few fixes to try:

  • Double-check variable assignment spacing
    Make sure you didn’t add spaces around the = when setting your variables. For example, num1 = 20 is wrong (it tries to run a command called num1 with arguments = and 20), but num1=20 is correct. Your code looks like it’s doing this right, but it’s worth double-checking if you accidentally added spaces when typing.

  • Ensure spaces around the + operator in expr
    The expr command is picky about whitespace around operators. If you wrote expr $num1+$num2 (no spaces around +), it’ll treat the whole string like a single non-integer argument. Your code uses expr $num1 + $num2 which is correct, but confirm there are actual spaces before and after the + in your terminal input—sometimes accidental typos happen here.

  • Verify your variables hold pure integers
    Run echo $num1 and echo $num2 to make sure they output just 20 and 5 with no extra spaces, newlines, or other characters. If, for example, num1 got set to " 20" (with a leading space) somehow, expr will reject it as a non-integer.

  • Switch to Bash’s built-in arithmetic expansion (easier alternative)
    If you want to avoid expr’s quirks entirely, Bash has its own way to do arithmetic that’s more forgiving. Just use this syntax instead:

    num1=20
    num2=5
    echo $((num1 + num2))
    

    This will output 25 without any of expr’s whitespace headaches.

Give these steps a try—chances are one of them will fix your error!

备注:内容来源于stack exchange,提问作者dhebbie Godspride

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 13:19:36