Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Advanced Bash Shell Scripting Gude.pdf
Скачиваний:
57
Добавлен:
17.08.2013
Размер:
4.82 Mб
Скачать

The Double Parentheses Construct

 

Advanced Bash-Scripting Guide:

 

Prev

Chapter 9. Variables Revisited

Next

9.7. The Double Parentheses Construct

Similar to the let command, the ((...)) construct permits arithmetic expansion and evaluation. In its simplest form, a=$(( 5 + 3 )) would set "a" to "5 + 3", or 8. However, this double parentheses construct is also a mechanism for allowing C-type manipulation of variables in Bash.

Example 9-25. C-type manipulation of variables

#!/bin/bash

# Manipulating a variable, C-style, using the ((...)) construct.

echo

 

 

 

(( a = 23 ))

# Setting a

value, C-style, with spaces on both sides of the "=".

echo "a (initial value) =

$a"

(( a++ ))

# Post-increment 'a', C-style.

echo "a (after a++) = $a"

 

(( a-- ))

# Post-decrement 'a', C-style.

echo "a (after a--) = $a"

 

(( ++a ))

# Pre-increment 'a', C-style.

echo "a (after ++a) = $a"

 

(( --a ))

# Pre-decrement 'a', C-style.

echo "a (after --a) = $a"

 

echo

 

 

 

(( t = a<45?7:11 ))

# C-style trinary operator.

echo "If a < 45, then t =

7, else t = 11."

echo "t = $t "

 

# Yes!

echo

#-----------------

#Easter Egg alert!

#-----------------

#Chet Ramey apparently snuck a bunch of undocumented C-style constructs #+ into Bash (actually adapted from ksh, pretty much).

#In the Bash docs, Ramey calls ((...)) shell arithmetic,

#+ but it goes far beyond that.

#Sorry, Chet, the secret is now out.

http://tldp.org/LDP/abs/html/dblparens.html (1 of 2) [7/15/2002 6:35:14 PM]

The Double Parentheses Construct

#See also "for" and "while" loops using the ((...)) construct.

#These work only with Bash, version 2.04 or later.

exit 0

See also Example 10-12.

Prev

Home

Next

$RANDOM: generate random integer

Up

Loops and Branches

http://tldp.org/LDP/abs/html/dblparens.html (2 of 2) [7/15/2002 6:35:14 PM]