Linux Bash Operators Like Assignment, Plus, Calculation – POFTUT

Linux Bash Operators Like Assignment, Plus, Calculation

[rps-include post=6835]

We can store different type of values in the variables. But just storing values is generally not enough and we need to make some operations with them. In this tutorial we will look different type of operators which will work with variables.

Assignment

The most used operator and generally underestimated its power. Variable assignment is made with =. By the way be aware that you do not confuse assignment operator with test operator which is the same =.

Let Operator

In bash scripting to make mathematical calculations and use operators let expression is used. let expression makes afterward expressions mathematically calculated. Look example below

Plus

To make sum operations + is used as expected but be aware of that let should be used.

Plus Equal

Plus equal is similar to plus but makes operations easy when one of the sum parameters are same for the result

Minus

Usage usage of the minus operator is the same with plus operator. As you know just the effect is different

Minus Equal

Plus equal is similar to plus but makes operations easy when one of the sum parameters are same for the result

Multiplication

You can find the multiplication operation below

LEARN MORE  Php - Numeric Variable Types Like Integer, Float, Double

Simple, isn’t is?

Division

You can find the division operation below

Exponentiation

Generally this type of operations are not required in bash scripting but to be aware of this operator we make simple example

Modulo

To get remainder of a division module is used. For example what will be the remainder after 5/3. We can use following code for this operation. As you will see below we have new keyword named expr

Bit-wise

Decimal numbers can be expressed in binary format. So if we need to shift the binary values of a decimal Bit-wise operator can be used.

In this example we have rotated binary for 2 times. Before the rotation binary value was like 0000010. After the rotation it is like 0001000 where its decimal value is 8. We have used = to make assignment easy.

Bit-wise operations can be done in reverse order like >>.

And

Logical operations are important for a programming language. Bash have full support for logical operations like AND.

[rps-include post=6835]

1 Response

  1. 08/08/2018

    […] Linux Bash Operators Like Assignment, Plus, Calculation […]

Leave a Reply

Your email address will not be published. Required fields are marked *