0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert Celsius to Fahrenheit

Use the formula F = (C * 9/5) + 32 in a Bash script like fahrenheit=$(echo "scale=2; ($celsius * 9/5) + 32" | bc) to convert Celsius to Fahrenheit.
📋

Examples

Input0
Output32.00
Input25
Output77.00
Input-40
Output-40.00
🧠

How to Think About It

To convert Celsius to Fahrenheit, multiply the Celsius value by 9, divide by 5, then add 32. In Bash, since it does not support floating-point math natively, use the external tool bc to perform the calculation and keep decimal precision.
📐

Algorithm

1
Get the Celsius temperature input from the user or variable
2
Calculate Fahrenheit using the formula: multiply Celsius by 9, divide by 5, then add 32
3
Use a tool like <code>bc</code> to handle decimal math in Bash
4
Print the Fahrenheit result
💻

Code

bash
#!/bin/bash
read -p "Enter temperature in Celsius: " celsius
fahrenheit=$(echo "scale=2; ($celsius * 9/5) + 32" | bc)
echo "$celsius Celsius is $fahrenheit Fahrenheit"
Output
Enter temperature in Celsius: 25 25 Celsius is 77.00 Fahrenheit
🔍

Dry Run

Let's trace converting 25 Celsius to Fahrenheit through the code

1

Input Celsius

User enters 25

2

Calculate Fahrenheit

Calculate (25 * 9/5) + 32 = 45 + 32 = 77.00 using bc

3

Print Result

Output: '25 Celsius is 77.00 Fahrenheit'

StepCelsiusCalculationFahrenheit
12525 * 9/5 + 3277.00
💡

Why This Works

Step 1: Formula for Conversion

The formula F = (C * 9/5) + 32 converts Celsius to Fahrenheit by scaling and shifting the temperature.

Step 2: Using bc for Decimal Math

Bash cannot do floating-point math directly, so bc is used to calculate with decimals and precision.

Step 3: Reading Input and Output

The script reads Celsius from the user, calculates Fahrenheit, then prints the result clearly.

🔄

Alternative Approaches

Using awk for calculation
bash
#!/bin/bash
read -p "Enter Celsius: " celsius
fahrenheit=$(awk "BEGIN {print ($celsius * 9/5) + 32}")
echo "$celsius Celsius is $fahrenheit Fahrenheit"
Awk can also do floating-point math without needing bc, making the script simpler.
Using integer math approximation
bash
#!/bin/bash
read -p "Enter Celsius: " celsius
fahrenheit=$(( (celsius * 9 / 5) + 32 ))
echo "$celsius Celsius is $fahrenheit Fahrenheit"
This uses integer math only, so it loses decimal precision but is simpler and faster.

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

Time Complexity

The script performs a fixed number of arithmetic operations and input/output steps, so it runs in constant time.

Space Complexity

It uses a few variables and no extra data structures, so space usage is constant.

Which Approach is Fastest?

Integer math is fastest but less precise; using bc or awk adds slight overhead but supports decimals accurately.

ApproachTimeSpaceBest For
Using bcO(1)O(1)Accurate decimal math
Using awkO(1)O(1)Simpler decimal math without bc
Integer mathO(1)O(1)Fast, approximate results
💡
Use bc or awk in Bash to handle decimal calculations like temperature conversion.
⚠️
Trying to do floating-point math directly in Bash without using bc or awk causes errors or wrong results.