27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15. Arithmetic Expansion<br />

Arithmetic expansion provides a powerful tool for performing arithmetic operations in scripts. Translating a<br />

string into a numerical expression is relatively straightforward using backticks, double parentheses, or let.<br />

Variations<br />

Arithmetic expansion with backticks (often used in conjunction with expr)<br />

z=`expr $z + 3`<br />

# The 'expr' command performs the expansion.<br />

Arithmetic expansion with double parentheses, and using let<br />

The use of backticks in arithmetic expansion has been superseded by double parentheses $((...))<br />

or the very convenient let construction.<br />

z=$(($z+3))<br />

# $((EXPRESSION)) is arithmetic expansion. # Not to be confused with<br />

#+ command substitution.<br />

let z=z+3<br />

let "z += 3" # Quotes permit the use of spaces and special operators.<br />

# The 'let' operator actually performs arithmetic evaluation,<br />

#+ rather than expansion.<br />

All the above are equivalent. You may use whichever one "rings your chimes".<br />

Examples of arithmetic expansion in scripts:<br />

1. Example 12−6<br />

2. Example 10−14<br />

3. Example 26−1<br />

4. Example 26−6<br />

5. Example A−18<br />

Chapter 15. Arithmetic Expansion 241

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!