V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lizhengbo
V2EX  ›  Linux

算术运算为什么加$

  •  
  •   lizhengbo · 2022-02-27 23:36:10 +08:00 · 2831 次点击
    这是一个创建于 826 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1 #!/bin/bash
    2 # Program:
    3 # Try do calculate 1+2+...+${you_input}
    4 # History:
    5 # 2015/07/17 VBird First release
    6 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    7 export PATH
    8
    9 read -p "Please input a number,I will count for 1+2+...+your_input:" nu
    10
    11 s=0
    12 for ((i=1; i<=${nu}; i=i+1))
    13 do
    14 s=$((${s}+${i}))
    15 done
    16 echo "The result of '1+2+3+...+${nu}' is ==> ${s}"

    -------------------------------------------------------

    为什么 14 行中的赋值算术运算需要在大括号前加$,不加就报错
    1 条回复
    lizhengbo
        1
    lizhengbo  
    OP
       2022-02-27 23:38:48 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5640 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:32 · PVG 11:32 · LAX 20:32 · JFK 23:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.