From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Working with numbers

Working with numbers - Bash Tutorial

From the course: Learning Bash Scripting

Working with numbers

- When we work with numbers in Bash we have two tools at our disposal, arithmetic expansion and arithmetic evaluation. Arithmetic expansion lets us use literal numbers or variables and perform mathematical operations on them. Giving us a result we can display or assign to a variable. Arithmetic evaluation makes changes to the value of an existing variable. Bash supports six basic arithmetic operators, addition with a plus, subtraction with a minus, multiplication with a single asterisk, division with a forward slash, modulo with a percent sign and exponentiation with double asterisks. And it's important to point out Bash can only do calculations with integers. We'll explore this limitation more in a little while. Let's take a moment to use arithmetic expansion here at the Bash prompt. I'll write echo, dollar sign, two opening parentheses, four plus four and two closing parentheses, that's eight. I'll write a few more of these…

Contents