双圆括号结构

与let命令很相似, ((...))结构允许算术扩展和赋值. 举个简单的例子, a=$(( 5 + 3 )), 将把变量"a"设为"5 + 3", 或者8. 然而, 双圆括号结构也被认为是在Bash中使用C语言风格变量操作的一种处理机制.

 

#!/bin/bash
(( a = 23 ))  # C语言风格的变量赋值,"="两边允许有空格.
echo "a (initial value) = $a"

(( a++ ))     # C语言风格的后置自加.
echo "a (after a++) = $a"

(( a-- ))     # C语言风格的后置自减.
echo "a (after a--) = $a"

(( ++a ))     # C语言风格的前置自加.
echo "a (after ++a) = $a"

(( --a ))     # C语言风格的前置自减.
echo "a (after --a) = $a"

(( t = a<45?7:11 ))   # C语言风格的三元操作.
echo "If a < 45, then t = 7, else t = 11."
echo "t = $t "
exit 0

结果:

root@ubuntu:~/resource/0510# ./test1 
a (initial value) = 23
a (after a++) = 24
a (after a--) = 23
a (after ++a) = 24
a (after --a) = 23
If a < 45, then t = 7, else t = 11.
t = 7 
root@ubuntu:~/resource/0510#


待续。。。。。