DEV Community

Mahbub Ferdous Bijoy
Mahbub Ferdous Bijoy

Posted on

Shell - Variable,Arithmetic Operator, Input

How to define variables

We can define a variable by using the syntax variable_name=value. To get the value of the variable, add $ before the variable.

Example:



shebang added 
#! /bin/bash

#here our variable example

greetings1=Hello
echo "ENTER YOUR NAME: "
read name
greetings2="How are you?"
echo "$greetings1 $name.$greetings2"



Enter fullscreen mode Exit fullscreen mode

Arithmetic Expressions

Below are the operators supported by bash for mathematical calculations:



1. +    = addition
2. -    = subtraction
3. /    = division
4. *    = exponentiation
5. %    = modulus


Enter fullscreen mode Exit fullscreen mode

 for arithmetic operations always make a variable like this:-  varName=$((operations))>>> additons=$((10+20))

Enter fullscreen mode Exit fullscreen mode

 you echo all arethmetic operations at a time its work first modulus than division than multiply than subtraction last it will works addition

Enter fullscreen mode Exit fullscreen mode

For decimal calculations:

we can use bc command to get the output to a particular number of decimal places. bc (Bash Calculator) is a command line calculator that supports calculation up to a certain number of decimal points.

echo "scale=2;22/7" | bc

Where scale defines the number of decimal places required in the output.

Examples:



#! /bin/bash

#for arithmetic operations always make a variable like this:-  varName=$((operations))>>> additons=$((10+20))

addition=$((10+10))
subtraction=$((10-8))
division=$((16/4))
exponentiation=$((10*2))
modulus=$((11%2))



Enter fullscreen mode Exit fullscreen mode


#here all arithmetic answer:

echo $addition      
#ans is 20
echo $subtraction   
#ans is 2
echo $division      
#ans is 4
echo $exponentiation  
#ans is 20
echo $modulus         
#ans is 1



Enter fullscreen mode Exit fullscreen mode

here is all functionalities addition:

when you echo all arithmetic operations at a time its work first modulus than division than multiply than subtraction last it will works addition


 $(($addition - $subtraction + $modulus * $exponentiation))

Enter fullscreen mode Exit fullscreen mode

Ans is 38 >> 1st(*) than (-) than (+)

for decimal calculations:

we can use bc command to get the output to a particular number of decimal places. bc (Bash Calculator) is a command line calculator that supports calculation up to a certain number of decimal points.


 "scale=2;22/7" | bc

Enter fullscreen mode Exit fullscreen mode

Ans will be - 3.14

Where scale defines the number of decimal places required in the output.

How to read user input:

Sometimes you'll need to gather user input and perform relevant operations.

In bash, we can take user input using the read command.

read variable_name

To prompt the user with a custom message, use the -p flag.

read -p "Enter your age" variable_name

Examples:



#! /bin/bash

read -p "ENTER YOUR NAME: " name
read -p "ENTER YOUR AGE: " age

echo "Your Name is : $name and your age : $age."



Enter fullscreen mode Exit fullscreen mode

Top comments (0)