Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 40 additions & 52 deletions concepts/arithmetic/about.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# About Arithmetic in the Bash Shell
# About Arithmetic in the bash Shell

Bash can do arithmetic but _only with integers_.
If you need to do floating-point math, you'll need to call out to some external program (like [awk][awk] or [bc][bc]).

The arithmetic operators are listed (in order of precedence) in the manual: [Shell Arithmetic][arithmetic].

Additionally, Bash only performs arithmetic in certain places (known as arithmetic "contexts").
These are the common ones, from highest to lowest precedence:

| Operators | Meaning |
| --- | --- |
| `**` | exponentiation |
| `*` `/` `%` | multiplication, division, remainder |
| `+` `-` | addition, subtraction |
| `<=` `>=` `<` `>` | comparison |
| `==` `!=` | equality, inequality |
| `&&` | logical AND |
| `\|\|` | logical OR |
| `=` `+=` `-=` `*=` `/=` `%=` | assignment |

Additionally, bash only performs arithmetic in certain places (known as arithmetic "contexts").

## Arithmetic Expansion

Expand All @@ -15,60 +28,36 @@ As we saw in the [Variables][variables] lesson, math can be performed in an arit
x=$(( 3 + 4 ))
```

## Arithmetic Conditional
## Arithmetic Command

The arithmetic conditional is performed within double parentheses _without_ the leading `$`:
The `(( expression ))` syntax is also used as a conditional, as described in the [Arithmetic Conditional][arithmetic-conditional] section of the Conditionals concept.

It can also be used as a standalone command to modify a variable's value.

```bash
(( expression ))
(( x = x + 1 ))
echo "new value for x is $x"
```

This does not expand to the result.
However it produces an _exit status_.

* The exit status is 0 (success) if the expression's result is **non-zero**.
* The exit status is 1 (failure) if the expression's result is **zero**.

~~~~exercism/note
Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true.
**This is the opposite of how Bash evaluates exit statuses!**
~~~~

This syntax is typically used in two places:

1. as the COMMAND part of an [`if` statement][conditional-if] or a [`while` loop][looping-while].
~~~~exercism/caution
Be careful using arithmetic this way combined with `set -e`.
If the expression evaluates to zero, the exit status will be non-zero and bash will terminate.

```bash
if (( x == y - 1 )); then ...
while (( y > 0 )); do ...
```

1. as a standalone command to modify a variable's value.

```bash
(( x = x + 1 ))
echo "new value for x is $x"
```

~~~~exercism/caution
Be careful using arithmetic this way combined with `set -e`.
If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate.

```bash
x=0
(( x++ ))
echo "you will see this"

set -e
x=0
(( x++ ))
echo "shell exits on the previous line; you won't see this"
```
```bash
x=0
(( x++ ))
echo "you will see this"

set -e
x=0
(( x++ ))
echo "shell exits on the previous line; you won't see this"
```

For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr]
For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr]

[incr]: https://thisvsthat.io/postincrement-vs-preincrement
~~~~
[incr]: https://thisvsthat.io/postincrement-vs-preincrement
~~~~


## Other Places With Arithmetic Context
Expand All @@ -95,7 +84,7 @@ There are a few other places that are arithmetic contexts:
* The offset and length parts of the `${var:offset:length}` expansion.
* The [`let` builtin command][let].
* Note that `(( expression ))` is preferred over `let "expresson"`.
Unlike the arithmetic conditional construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting.
Unlike the [arithmetic conditional][arithmetic-conditional] construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting.
* Variable assignment, when the variable has the "integer attribute".

```bash
Expand Down Expand Up @@ -149,16 +138,15 @@ s="hello world"
len=$(( ${#s} * 2 ))
```

And positional paremeters have to as well.
Positional parameters also need the `$`.
If the first two positional parameters represent width and height:

```bash
area=$(( $1 * $2 ))
```

[arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic
[conditional-if]: https://exercism.org/tracks/bash/concepts/conditionals#h-the-if-command
[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops
[arithmetic-conditional]: https://exercism.org/tracks/bash/concepts/conditionals#h-arithmetic-conditional
[looping-for]: https://exercism.org/tracks/bash/concepts/looping#h-arithmetic-for-loop
[arrays]: https://exercism.org/tracks/bash/concepts/arrays
[let]: https://www.gnu.org/software/bash/manual/bash.html#index-let
Expand Down
92 changes: 40 additions & 52 deletions concepts/arithmetic/introduction.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# Introduction to Arithmetic in the Bash Shell
# Introduction to Arithmetic in the bash Shell

Bash can do arithmetic but _only with integers_.
If you need to do floating-point math, you'll need to call out to some external program (like [awk][awk] or [bc][bc]).

The arithmetic operators are listed (in order of precedence) in the manual: [Shell Arithmetic][arithmetic].

Additionally, Bash only performs arithmetic in certain places (known as arithmetic "contexts").
These are the common ones, from highest to lowest precedence:

| Operators | Meaning |
| --- | --- |
| `**` | exponentiation |
| `*` `/` `%` | multiplication, division, remainder |
| `+` `-` | addition, subtraction |
| `<=` `>=` `<` `>` | comparison |
| `==` `!=` | equality, inequality |
| `&&` | logical AND |
| `\|\|` | logical OR |
| `=` `+=` `-=` `*=` `/=` `%=` | assignment |

Additionally, bash only performs arithmetic in certain places (known as arithmetic "contexts").

## Arithmetic Expansion

Expand All @@ -15,60 +28,36 @@ As we saw in the [Variables][variables] lesson, math can be performed in an arit
x=$(( 3 + 4 ))
```

## Arithmetic Conditional
## Arithmetic Command

The arithmetic conditional is performed within double parentheses _without_ the leading `$`:
The `(( expression ))` syntax is also used as a conditional, as described in the [Arithmetic Conditional][arithmetic-conditional] section of the Conditionals concept.

It can also be used as a standalone command to modify a variable's value.

```bash
(( expression ))
(( x = x + 1 ))
echo "new value for x is $x"
```

This does not expand to the result.
However it produces an _exit status_.

* The exit status is 0 (success) if the expression's result is **non-zero**.
* The exit status is 1 (failure) if the expression's result is **zero**.

~~~~exercism/note
Inside `((` Bash uses the same sense of true/false as the C language: zero is false, non-zero is true.
**This is the opposite of how Bash evaluates exit statuses!**
~~~~

This syntax is typically used in two places:

1. as the COMMAND part of an [`if` statement][conditional-if] or a [`while` loop][looping-while].
~~~~exercism/caution
Be careful using arithmetic this way combined with `set -e`.
If the expression evaluates to zero, the exit status will be non-zero and bash will terminate.

```bash
if (( x == y - 1 )); then ...
while (( y > 0 )); do ...
```

1. as a standalone command to modify a variable's value.

```bash
(( x = x + 1 ))
echo "new value for x is $x"
```

~~~~exercism/caution
Be careful using arithmetic this way combined with `set -e`.
If the expression evaluates to zero, the exit status will be non-zero and Bash will terminate.

```bash
x=0
(( x++ ))
echo "you will see this"

set -e
x=0
(( x++ ))
echo "shell exits on the previous line; you won't see this"
```
```bash
x=0
(( x++ ))
echo "you will see this"

set -e
x=0
(( x++ ))
echo "shell exits on the previous line; you won't see this"
```

For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr]
For a more thorough discussion about the `++` operator, see this article on [Postincrement vs Preincrement][incr]

[incr]: https://thisvsthat.io/postincrement-vs-preincrement
~~~~
[incr]: https://thisvsthat.io/postincrement-vs-preincrement
~~~~


## Other Places With Arithmetic Context
Expand All @@ -95,7 +84,7 @@ There are a few other places that are arithmetic contexts:
* The offset and length parts of the `${var:offset:length}` expansion.
* The [`let` builtin command][let].
* Note that `(( expression ))` is preferred over `let "expresson"`.
Unlike the arithmetic conditional construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting.
Unlike the [arithmetic conditional][arithmetic-conditional] construct, the `let` expression is subject to filename-expansion and word-splitting, and it requires quoting.
* Variable assignment, when the variable has the "integer attribute".

```bash
Expand Down Expand Up @@ -149,16 +138,15 @@ s="hello world"
len=$(( ${#s} * 2 ))
```

And positional paremeters have to as well.
Positional parameters also need the `$`.
If the first two positional parameters represent width and height:

```bash
area=$(( $1 * $2 ))
```

[arithmetic]: https://www.gnu.org/software/bash/manual/bash.html#Shell-Arithmetic
[conditional-if]: https://exercism.org/tracks/bash/concepts/conditionals#h-the-if-command
[looping-while]: https://exercism.org/tracks/bash/concepts/looping#h-while-loops
[arithmetic-conditional]: https://exercism.org/tracks/bash/concepts/conditionals#h-arithmetic-conditional
[looping-for]: https://exercism.org/tracks/bash/concepts/looping#h-arithmetic-for-loop
[arrays]: https://exercism.org/tracks/bash/concepts/arrays
[let]: https://www.gnu.org/software/bash/manual/bash.html#index-let
Expand Down
Loading
Loading