products:ict:linux:shell_scripting:arithmetic
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| products:ict:linux:shell_scripting:arithmetic [2023/05/04 17:04] – wikiadmin | products:ict:linux:shell_scripting:arithmetic [2023/05/04 17:13] (current) – wikiadmin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | |||
| + | ====== Shell Scripting Arithmetic ====== | ||
| + | |||
| + | |||
| let is a builtin function of Bash that allows us to do simple arithmetic. It follows the basic format: | let is a builtin function of Bash that allows us to do simple arithmetic. It follows the basic format: | ||
| Line 151: | Line 155: | ||
| In the section on Variables we saw that we could save the output of a command easily to a variable. It turns out that this mechanism is also able to do basic arithmetic for us if we tweak the syntax a little. We do so by using double brackets like so: | In the section on Variables we saw that we could save the output of a command easily to a variable. It turns out that this mechanism is also able to do basic arithmetic for us if we tweak the syntax a little. We do so by using double brackets like so: | ||
| + | < | ||
| $(( expression )) | $(( expression )) | ||
| + | </ | ||
| Here's an example to illustrate: | Here's an example to illustrate: | ||
| Line 161: | Line 167: | ||
| # Basic arithmetic using double parentheses | # Basic arithmetic using double parentheses | ||
| + | < | ||
| a=$(( 4 + 5 )) | a=$(( 4 + 5 )) | ||
| + | </ | ||
| echo $a # 9 | echo $a # 9 | ||
| + | < | ||
| a=$((3+5)) | a=$((3+5)) | ||
| + | </ | ||
| echo $a # 8 | echo $a # 8 | ||
| + | < | ||
| b=$(( a + 3 )) | b=$(( a + 3 )) | ||
| + | </ | ||
| echo $b # 11 | echo $b # 11 | ||
| + | < | ||
| b=$(( $a + 4 )) | b=$(( $a + 4 )) | ||
| + | </ | ||
| echo $b # 12 | echo $b # 12 | ||
| + | < | ||
| (( b++ )) | (( b++ )) | ||
| + | </ | ||
| echo $b # 13 | echo $b # 13 | ||
| + | < | ||
| (( b += 3 )) | (( b += 3 )) | ||
| + | </ | ||
| echo $b # 16 | echo $b # 16 | ||
| + | < | ||
| a=$(( 4 * 5 )) | a=$(( 4 * 5 )) | ||
| + | </ | ||
| echo $a # 20 | echo $a # 20 | ||
| Line 191: | Line 211: | ||
| Let's break it down: | Let's break it down: | ||
| + | < | ||
| a=$(( 4 + 5 )) | a=$(( 4 + 5 )) | ||
| + | </ | ||
| This is the basic format. As you can see we may space it out nicely for readability without the need for quotes. | This is the basic format. As you can see we may space it out nicely for readability without the need for quotes. | ||
| + | < | ||
| a=$((3+5)) | a=$((3+5)) | ||
| + | </ | ||
| As you can see, it works just the same if we take spacing out. | As you can see, it works just the same if we take spacing out. | ||
| + | < | ||
| b=$(( a + 3 )) | b=$(( a + 3 )) | ||
| + | </ | ||
| We may include variables without the preceding $ sign. | We may include variables without the preceding $ sign. | ||
| + | < | ||
| b=$(( $a + 4 )) | b=$(( $a + 4 )) | ||
| + | </ | ||
| Variables can be included with the $ sign if you prefer. | Variables can be included with the $ sign if you prefer. | ||
| + | < | ||
| (( b++ )) | (( b++ )) | ||
| + | </ | ||
| This is a slightly different form. Here the value of the variable b is incremented by 1 (using the same mechanism illustrated under let). When we do this we don't need the $ sign preceding the brackets. | This is a slightly different form. Here the value of the variable b is incremented by 1 (using the same mechanism illustrated under let). When we do this we don't need the $ sign preceding the brackets. | ||
| + | < | ||
| (( b += 3 )) | (( b += 3 )) | ||
| + | </ | ||
| This is a slightly different form of the previous example. Here the value of the variable b is incremented by 3. It is a shorthand for b = b + 3. | This is a slightly different form of the previous example. Here the value of the variable b is incremented by 3. It is a shorthand for b = b + 3. | ||
| Line 275: | Line 306: | ||
| print out the result of the expression. | print out the result of the expression. | ||
| + | < | ||
| $(( expression )) | $(( expression )) | ||
| + | </ | ||
| Return the result of the expression. | Return the result of the expression. | ||
products/ict/linux/shell_scripting/arithmetic.1683201875.txt.gz · Last modified: 2023/05/04 17:04 by wikiadmin