A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://ryanstutorials.net/bash-scripting-tutorial/bash-arithmetic.php below:

Arithmetic - Bash Scripting Tutorial

Arithmetic!

It all adds up.

Introduction

Depending on what type of work you want your scripts to do you may end up using arithmetic a lot or not much at all. It's a reasonable certainty however that you will need to use arithmetic at some point. Like variables, they are reasonably easy to implement and knowing how to do so is an essential skill in Bash scripting mastery.

There are several ways to go about arithmetic in Bash scripting. We'll cover them for completeness but the recommended approach is arithmetic expansion (covered last).

Let

let is a builtin function of Bash that allows us to do simple arithmetic. It follows the basic format:

let <arithmetic expression>

The arithmetic expression can take a variety of formats which we'll outline below. The first part is generally always a variable which the result is saved into however.

Let's look at a simple example:

let_example.sh
  1. #!/bin/bash
  2. let a=5+4
  3. echo $a
  4. let "a = 5 + 4"
  5. echo $a
  6. let a++
  7. echo $a
  8. let "a = 4 * 5"
  9. echo $a
  10. let "a = $1 + 30"
  11. echo $a

Let's break it down:

  1. ./let_example.sh 15
  2. 9
  3. 9
  4. 10
  5. 20
  6. 45

Here is a table with some of the basic expressions you may perform. There are others but these are the most commonly used.

+, -, \*, / addition, subtraction, multiply, divide var++ Increase the variable var by 1 var-- Decrease the variable var by 1 % Modulus (Return the remainder after division)

These operators may be used in the other mechanisms described below as well.

Expr

expr is similar to let except instead of saving the result to a variable it instead prints the answer. Unlike let you don't need to enclose the expression in quotes. You also must have spaces between the items of the expression. It is also common to use expr within command substitution to save the output to a variable.

expr item1 operator item2

Let's look at a simple example:

expr_example.sh
  1. #!/bin/bash
  2. expr 5 + 4
  3. expr "5 + 4"
  4. expr 5+4
  5. expr 5 \* $1
  6. expr 11 % 2
  7. a=$( expr 10 - 3 )
  8. echo $a

Let's break it down:

  1. ./expr_example.sh 12
  2. 9
  3. 5 + 4
  4. 5+4
  5. 60
  6. 1
  7. 7
Double Parentheses

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 ))

Here's an example to illustrate:

expansion_example.sh
  1. #!/bin/bash
  2. a=$(( 4 + 5 ))
  3. echo $a
  4. a=$((3+5))
  5. echo $a
  6. b=$(( a + 3 ))
  7. echo $b
  8. b=$(( $a + 4 ))
  9. echo $b
  10. (( b++ ))
  11. echo $b
  12. (( b += 3 ))
  13. echo $b
  14. a=$(( 4 * 5 ))
  15. echo $a

Let's break it down:

  1. ./expansion_example.sh
  2. 9
  3. 8
  4. 11
  5. 12
  6. 13
  7. 16
  8. 20

So as you can see double parenthese is quite flexible in how you format it's expression. This is part of why we prefer this method. As double parentheses is builtin to Bash it also runs slighly more efficiently (though to be honest, with the raw computing power of machines these days the difference in performance is really insignificant).

Length of a Variable

This isn't really arithmetic but it can be quite useful. If you want to find out the lengh of a variable (how many characters) you can do the following:

${#variable}

Here's an example:

length_example.sh
  1. #!/bin/bash
  2. a='Hello World'
  3. echo ${#a}
  4. b=4953
  5. echo ${#b}
Summary
let expression
Make a variable equal to an expression.
expr expression
print out the result of the expression.
$(( expression ))
Return the result of the expression.
${#var}
Return the length of the variable var.
Arithmetic
There are several ways in which to do arithmetic in Bash scripts. Double parentheses is the preferred method.
Formatting
When doing arithmetic, the presence or absence of spaces (and quotes) is often important.
Activities

Let's dive in with arithmetic.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4