Bash Script to Find Smallest of Three Numbers
Use a Bash script with
if statements to compare three numbers and print the smallest, like: if [ "$a" -le "$b" ] && [ "$a" -le "$c" ]; then echo "$a"; elif [ "$b" -le "$a" ] && [ "$b" -le "$c" ]; then echo "$b"; else echo "$c"; fi.Examples
Input3 5 1
Output1
Input10 10 10
Output10
Input-2 0 -5
Output-5
How to Think About It
To find the smallest of three numbers, compare the first number with the other two using
-le (less or equal) operator. If it is smaller or equal to both, it is the smallest. Otherwise, compare the second number with the other two. If neither the first nor second is smallest, the third must be the smallest.Algorithm
1
Get the three numbers as input.2
Check if the first number is less than or equal to both the second and third numbers.3
If yes, return the first number as smallest.4
Otherwise, check if the second number is less than or equal to both the first and third numbers.5
If yes, return the second number as smallest.6
Otherwise, return the third number as smallest.Code
bash
#!/bin/bash read -p "Enter three numbers separated by space: " a b c if [ "$a" -le "$b" ] && [ "$a" -le "$c" ]; then echo "$a" elif [ "$b" -le "$a" ] && [ "$b" -le "$c" ]; then echo "$b" else echo "$c" fi
Output
1
Dry Run
Let's trace input '3 5 1' through the code
1
Input values
a=3, b=5, c=1
2
Check if a <= b and a <= c
3 <= 5 is true, 3 <= 1 is false, so condition false
3
Check if b <= a and b <= c
5 <= 3 is false, so condition false
4
Else case
Print c = 1
| Step | Condition | Result |
|---|---|---|
| 1 | 3 <= 5 and 3 <= 1 | false |
| 2 | 5 <= 3 and 5 <= 1 | false |
| 3 | Print c | 1 |
Why This Works
Step 1: Compare first number
We use -le to check if the first number is less than or equal to the other two numbers.
Step 2: Compare second number
If the first is not smallest, we check if the second number is less than or equal to the other two.
Step 3: Default to third number
If neither first nor second is smallest, the third number must be the smallest.
Alternative Approaches
Using array and sort
bash
#!/bin/bash read -p "Enter three numbers: " a b c arr=($a $b $c) sorted=($(printf "%s\n" "${arr[@]}" | sort -n)) echo "${sorted[0]}"
This method uses sorting but is less efficient for just three numbers.
Using arithmetic expansion
bash
#!/bin/bash read -p "Enter three numbers: " a b c min=$a if (( b < min )); then min=$b; fi if (( c < min )); then min=$c; fi echo "$min"
This uses arithmetic comparison which is simpler and more readable.
Complexity: O(1) time, O(1) space
Time Complexity
The script performs a fixed number of comparisons (at most two), so it runs in constant time.
Space Complexity
Only a few variables are used to store input and intermediate results, so space is constant.
Which Approach is Fastest?
Direct comparisons with if statements are fastest and simplest; sorting is slower and unnecessary for just three numbers.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct if comparisons | O(1) | O(1) | Small fixed inputs, fastest |
| Array sort | O(n log n) | O(n) | When needing sorted output, less efficient here |
| Arithmetic expansion | O(1) | O(1) | Simple numeric comparisons, readable |
Use
-le for numeric comparison in Bash, not == or = which are for strings.Beginners often use
== or = for numeric comparison, which compares strings and causes errors.