1. Numeric variables in Bash
As data people, I am sure you want to learn about using numeric variables in Bash. Let's get started!
2. Numbers in other languages
Working with numeric variables is not natively built into Bash as it is in other languages.
In R or Python REPL you may be used to just typing arithmetic like this.
And getting the result you want.
3. Numbers in the shell
You cannot natively do arithmetic in Bash.
If you try the following in the Bash terminal.
You will get an error like this.
4. Introducing expr
A useful command-line program is expr. This is a program just like cat and grep which can be used for basic arithmetic.
Now if we do the following in a terminal,
we get the result we want.
Nice!
5. expr limitations
Expr has a big limitation though. It cannot handle decimal places.
If you do the following in the terminal,
this will be the error you get.
However, there is a solution!
6. Introducing bc
Another useful command-line program is bc which stands for 'basic calculator'.
You can enter bc just like a REPL in the command line.
In this picture you can see bc being invoked, entering five plus seven, which returns twelve. Then the word quit exits back to the shell.
7. Getting numbers to bc
You can use bc without opening the program using piping since bc accepts arguments of numbers and operations.
Here we send the string five plus seven point five into bc using a pipe.
The return is exactly what we want, the decimal number returned.
8. bc scale argument
bc also has a useful argument 'scale' which allows you to specify how many decimal places for the return value.
Here we know the answer is three point three repeated. But by default, bc will return the value three for this division.
Let's see what happens if we echo in the scale argument. Note the use of the semi-colon to separate lines in the terminal.
Now, we get the result we were expecting to three decimal places.
9. Numbers in Bash scripts
You can assign numeric variables much the same way as the string variables you did previously. Remember not to have spaces around the equals sign.
In this example, we set a string variable of a dog's name and a numeric variable of the dogs age.
You could have also used quotes around the six, but this would make it a string variable so may limit what you could do later!
Then we echo the sentence noted. Since we used double quotes, we can use dollar-sign notation to interpolate the variable names within the echo call.
This returns the correct sentence.
10. Double bracket notation
There is a special variant on the single bracket notation discussed in the previous lesson for numeric variables.
Here we use expr to add two numbers. However, we can achieve the same result using double parentheses, as seen here.
We get back the same result for both calls.
Beware this method uses expr, not bc. So you cannot use decimals with this method.
11. Shell within a shell revisited
Do you remember when we used single parentheses or backticks to create a shell-within-a-shell in the last lesson?
This is very useful for numeric variables and calculations.
Here we create two variables. Some accuracy scores for two models.
We can use the single bracket notation to create a shell-within-a-shell, where we echo the sum of these to bc.
On the next line, we do something more complex, building on the first line and adding a division after the sum to get the average. Again, the shell-within-a-shell returns the value inside the echo call.
We see the sum and average printed out in-line.
12. Let's practice!
Let's practice using numeric variables in Bash scripts.