- [Instructor] Math with the Shell.
In the previous video we learned about the function
to prepend to environment variables.
Arithmetic operations are an essential requirement
for every programming language.
So, in this video we'll explore various methods
for performing arithmetic operations in Shell.
The Bash Shell environment can perform
basic arithmetic operations using the command let,
open bracket, open bracket,
close bracket, close bracket, and square brackets.
The two utilities expr and bc are also very helpful
in performing advanced operations.
A numeric value can be assigned
as a regular variable assignment,
which is stored as a string.
However, we use methods to manipulate those numbers.
The let command can be used to perform
basic operations directly.
While using let, we can use variable names
without the dollar prefix, for example:
increment operation is, decrement operation is, shorthands.
These are equal to let no equals no plus six,
and let no equals no minus six, respectively.
Now we'll see some alternate methods.
The square brackets operator can be used
in the same way as the let command, as.
Using the dollar prefix inside
square bracket operators are legal.
For example, open bracket, open bracket,
close bracket, close bracket can also be used.
Dollar prefix with a variable name
is used when open bracket, open bracket,
close bracket, close bracket operator is used, as.
Expr can also be used for basic operations.
All of the preceding methods do not support
floating point numbers, and operate on integers only.
Bc, the precision calculator, is an advanced utility
for mathematical operations.
It has a wide range of options.
We can perform floating point operations
and use advanced functions as.
Additional parameters can be passed to bc
with prefixes to the operation,
with semicolon as delimiters, through STDIN.
Decimal places scale with bc.
For example, the scale equals two parameters,
sets the number of decimal places to two.
Hence the output of bc will contain a number
with two decimal places.
Base conversion with bc.
We can convert from one base number system to another.
Let's convert from decimal to binary,
and binary to octal.
Using the following code, I've pre-coded the file.
When run, this will get you the output
as binary value of the number 100.
Now, we'll convert this into a decimal number.
Calculating squares and square roots can be done as.
Awesome.
In this video, we've seen how to math with the Shell.
In the next video, we'll learn how to play
with file descriptors and redirection.