0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Area of Rectangle with Input and Output

Use a Bash script that reads length and width using read, then calculates area with area=$((length * width)) and prints it with echo.
📋

Examples

Inputlength=5, width=3
OutputArea of rectangle: 15
Inputlength=10, width=0
OutputArea of rectangle: 0
Inputlength=7, width=7
OutputArea of rectangle: 49
🧠

How to Think About It

To find the area of a rectangle, you multiply its length by its width. The script should ask the user for these two values, then multiply them using Bash arithmetic, and finally show the result.
📐

Algorithm

1
Prompt the user to enter the length of the rectangle.
2
Read the length input.
3
Prompt the user to enter the width of the rectangle.
4
Read the width input.
5
Calculate the area by multiplying length and width.
6
Print the calculated area.
💻

Code

bash
#!/bin/bash

echo "Enter length of rectangle:"
read length

echo "Enter width of rectangle:"
read width

area=$((length * width))
echo "Area of rectangle: $area"
Output
Enter length of rectangle: 5 Enter width of rectangle: 3 Area of rectangle: 15
🔍

Dry Run

Let's trace the example where length=5 and width=3 through the code

1

Read length

User inputs 5, so length=5

2

Read width

User inputs 3, so width=3

3

Calculate area

area=$((5 * 3)) = 15

lengthwidtharea
5315
💡

Why This Works

Step 1: Reading inputs

The script uses read to get user input for length and width, storing them in variables.

Step 2: Calculating area

It uses Bash arithmetic $(( )) to multiply length and width to find the area.

Step 3: Displaying result

The script prints the area with echo so the user sees the output.

🔄

Alternative Approaches

Using command line arguments
bash
#!/bin/bash

length=$1
width=$2
area=$((length * width))
echo "Area of rectangle: $area"
This method takes length and width as arguments when running the script, useful for automation but less interactive.
Using bc for floating point
bash
#!/bin/bash

echo "Enter length:"
read length

echo "Enter width:"
read width

area=$(echo "$length * $width" | bc)
echo "Area of rectangle: $area"
This approach supports decimal numbers using bc, unlike Bash arithmetic which only supports integers.

Complexity: O(1) time, O(1) space

Time Complexity

The script performs a fixed number of operations regardless of input size, so it runs in constant time.

Space Complexity

It uses a few variables to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

All approaches run instantly for this simple calculation; using Bash arithmetic is fastest for integers, while bc adds overhead but supports decimals.

ApproachTimeSpaceBest For
Interactive input with readO(1)O(1)Simple integer input
Command line argumentsO(1)O(1)Automation and scripts
Using bc for floating pointO(1)O(1)Decimal calculations
💡
Always validate inputs to ensure they are positive numbers before calculating area.
⚠️
Forgetting to use double parentheses $(( )) for arithmetic causes the script to treat inputs as strings.