Module Title: dgt_bash03 – Arithmetic expressions
Module Description:
The module “dgt_bash03 – Arithmetic expressions” covers arithmetic expressions in Bash scripting. It is intended for people working with shell scripts, from beginners to experienced users, and covers numerical operations within scripts in detail.
The theory covers how Bash evaluates numbers, operators and precedence; the practice is carried out entirely in natural language. You tell the machine what has to be calculated (a percentage, a counter, a ratio with decimals) and under which constraints, then you read the expression it chose, check whether integer division or operator precedence could silently give the wrong answer, and test it with edge cases until the result is right.
Module Objectives:
- Understand the basics of arithmetic expressions in Bash.
- Learn the different methods Bash offers to declare and perform arithmetic operations, and when each one is appropriate.
- Gain practical experience by asking the machine to solve concrete calculations and verifying its scripts.
- Understand the techniques for handling complex mathematical calculations within shell scripts, so you can demand them and recognise them.
- Develop the ability to spot inefficient or erroneous arithmetic expressions in generated code and explain how to fix them.
Key Topics Covered:
- Introduction to Arithmetic in Bash
- Overview of why arithmetic operations are essential in scripting.
-
Basic syntax and structure of arithmetic expressions.
-
Arithmetic Operators in Bash
- Detailed exploration of supported operators: addition, subtraction, multiplication, division, modulus, etc.
-
Understanding operator precedence and how it affects calculations.
-
Methods for Performing Arithmetic Operations
- Using the
letcommand for simple calculations. - Employing the double parentheses
syntax for more complex expressions. - Utilizing
$syntax for arithmetic evaluation within scripts. -
Exploring alternative approaches like
exprand external tools, e.g.bc, for specialized operations. -
Examples of Arithmetic Declaration in Bash
-
Examples of what the machine produces for each method, which you must be able to read and verify:
- Using
let:
bash
let "a = 5 + 3"
echo $a - Double Parentheses:
bash
b = a * 2
echo $b $Syntax:
bash
c=$ b / 4
echo $c
- Using
-
Advanced Arithmetic Techniques
- Handling floating-point arithmetic.
- Integrating external tools for complex calculations.
-
Error handling and validation in arithmetic operations.
-
Best Practices and Optimization
- Recognising clean, readable and maintainable code, and asking for it explicitly.
- Performance considerations when using different methods.
- Diagnosing common issues in arithmetic expressions written by the machine.
Module Format:
- Interactive lectures with live demonstrations of how Bash evaluates expressions.
- Natural-language labs where participants instruct the machine to write and test scripts, then review and correct its work.
- Real-world examples to illustrate the application of the concepts learned.
- Quizzes and assignments to reinforce learning and assess understanding.
- An online community for ongoing discussion and troubleshooting.
By the end of this module, you will understand how Bash performs a wide range of arithmetic operations and will be able to direct a machine to use them correctly and efficiently. This knowledge applies both to task automation and to complex scripts, and is what you need to check, and when necessary correct, what the machine delivers.
The students can push their exercises to the Academy DevOps & SRE Git project. For this module, create a folder with your username as its name in the following subfolder: https://github.com/Garanti-Del-Talento/gdt_academy
