Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lizhengbo
V2EX  ›  Linux

算术运算为什么加$

  •  
  •   lizhengbo · Feb 27, 2022 · 3298 views
    This topic created in 1538 days ago, the information mentioned may be changed or developed.
    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 行中的赋值算术运算需要在大括号前加$,不加就报错
    lizhengbo
        1
    lizhengbo  
    OP
       Feb 27, 2022
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3019 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 05:34 · PVG 13:34 · LAX 22:34 · JFK 01:34
    ♥ Do have faith in what you're doing.