0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Largest of Three Numbers

Use a Bash script with if statements to compare three numbers and print the largest, like: if [ "$a" -ge "$b" ] && [ "$a" -ge "$c" ]; then echo $a; elif [ "$b" -ge "$a" ] && [ "$b" -ge "$c" ]; then echo $b; else echo $c; fi.
📋

Examples

Inputa=5, b=3, c=9
Output9
Inputa=10, b=10, c=2
Output10
Inputa=7, b=7, c=7
Output7
🧠

How to Think About It

To find the largest number among three, compare the first number with the other two using if conditions. If it is greater or equal to both, it is the largest. Otherwise, compare the second number similarly. If neither the first nor second is largest, the third must be the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Check if the first number is greater than or equal to the second and third.
3
If yes, return the first number as largest.
4
Else, check if the second number is greater than or equal to the first and third.
5
If yes, return the second number as largest.
6
Otherwise, return the third number as largest.
💻

Code

bash
#!/bin/bash

a=5
b=3
c=9

if [ "$a" -ge "$b" ] && [ "$a" -ge "$c" ]; then
  echo "$a"
elif [ "$b" -ge "$a" ] && [ "$b" -ge "$c" ]; then
  echo "$b"
else
  echo "$c"
fi
Output
9
🔍

Dry Run

Let's trace the example where a=5, b=3, c=9 through the code

1

Compare a with b and c

Check if 5 >= 3 and 5 >= 9 (false because 5 is not >= 9)

2

Compare b with a and c

Check if 3 >= 5 and 3 >= 9 (false because 3 is not >= 5)

3

Else case

Since neither a nor b is largest, print c which is 9

StepConditionResult
15 >= 3 and 5 >= 9false
23 >= 5 and 3 >= 9false
3Print c9
💡

Why This Works

Step 1: Compare first number

The script uses -ge to check if the first number is greater than or equal to the other two.

Step 2: Compare second number

If the first is not largest, it checks if the second number is greater than or equal to the others.

Step 3: Default to third number

If neither first nor second is largest, the script prints the third number as the largest.

🔄

Alternative Approaches

Using nested if-else
bash
#!/bin/bash

a=5
b=3
c=9

if [ "$a" -ge "$b" ]; then
  if [ "$a" -ge "$c" ]; then
    echo "$a"
  else
    echo "$c"
  fi
else
  if [ "$b" -ge "$c" ]; then
    echo "$b"
  else
    echo "$c"
  fi
fi
This approach uses nested conditions, which can be easier to read but slightly longer.
Using array and loop
bash
#!/bin/bash

nums=(5 3 9)
largest=${nums[0]}
for num in "${nums[@]}"; do
  if [ "$num" -gt "$largest" ]; then
    largest=$num
  fi
done
echo "$largest"
This method uses a loop to find the largest, useful if you want to extend to more numbers.

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

Time Complexity

The script performs a fixed number of comparisons (3), so it runs in constant time O(1).

Space Complexity

It uses a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using direct comparisons is simplest, while loops add flexibility for more numbers.

ApproachTimeSpaceBest For
Direct if-elseO(1)O(1)Simple fixed number comparisons
Nested if-elseO(1)O(1)Clearer logic for beginners
Loop with arrayO(n)O(n)Finding largest in many numbers
💡
Always quote variables in Bash test conditions to avoid errors with empty or special values.
⚠️
Forgetting to use -ge or mixing up numeric and string comparisons causes wrong results.