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

Arithmetic Expansion

Advanced Bash-Scripting Guide:

Prev

Next

Chapter 15. Arithmetic Expansion

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

Variations

Arithmetic expansion with backticks (often used in conjunction with expr)

z=`expr $z + 3` # 'expr' does the expansion.

Arithmetic expansion with double parentheses, and using let

The use of backticks in arithmetic expansion has been superseded by double parentheses $((...)) or the very convenient let construction.

z=$(($z+3))

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

#command substitution.

let z=z+3

let "z += 3" #If quotes, then spaces and special operators allowed.

# 'let' is actually arithmetic evaluation, rather than expansion.

All the above are equivalent. You may use whichever one "rings your chimes".

Examples of arithmetic expansion in scripts:

1.Example 12-6

2.Example 10-14

3.Example 26-1

4.Example 26-4

5.Example A-17

Prev

Home

Next

Command Substitution

Up

I/O Redirection

http://tldp.org/LDP/abs/html/arithexp.html [7/15/2002 6:34:33 PM]