Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334852
  • 博文数量: 115
  • 博客积分: 1019
  • 博客等级: 准尉
  • 技术积分: 1104
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-22 15:02
个人简介

别想万里,要把一只脚放到另一脚的前边

文章分类

全部博文(115)

文章存档

2018年(1)

2015年(2)

2014年(31)

2013年(38)

2012年(43)

我的朋友

分类: LINUX

2013-12-27 16:31:54

Chapter 13. Arithmetic Expansion

Arithmetic expansion provides a powerful tool for performing (integer) arithmetic operations in scripts. Translating a string into a numerical expression is relatively straightforward using backticks, double parentheses, or let.

Variations

Arithmetic expansion with (often used in conjunction with )
z=`expr $z + 3`          # The 'expr' command performs the expansion.
Arithmetic expansion with , and using

The use of backticks (backquotes) in arithmetic expansion has been superseded by double parentheses -- ((...)) and $((...)) -- and also by the very convenient construction.


z=$(($z+3))
z=$((z+3))                                  #  Also correct.
                                            #  Within double parentheses,
                                            #+ parameter dereferencing
                                            #+ is optional.

# $((EXPRESSION)) is arithmetic expansion.  #  Not to be confused with
                                            #+ command substitution.



# You may also use operations within double parentheses without assignment.

  n=0
  echo "n = $n"                             # n = 0

  (( n += 1 ))                              # Increment.
# (( $n += 1 )) is incorrect!
  echo "n = $n"                             # n = 1


let z=z+3
let "z += 3"  #  Quotes permit the use of spaces in variable assignment.
              #  The 'let' operator actually performs arithmetic evaluation,
              #+ rather than expansion.


阅读(462) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~