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))

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

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

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

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.

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

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

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Timescale

πŸ“Š Benchmarking Databases for Real-Time Analytics Applications

Benchmarking Timescale, Clickhouse, Postgres, MySQL, MongoDB, and DuckDB for real-time analytics. Introducing RTABench πŸš€

Read full post β†’

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay