0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Sum of Even Numbers

Use a Bash script that loops through numbers, checks if each number is even with if (( num % 2 == 0 )), and adds it to a sum variable; for example: sum=0; for num in {1..10}; do if (( num % 2 == 0 )); then ((sum+=num)); fi; done; echo $sum.
📋

Examples

Input1 2 3 4 5
Output6
Input10 15 20 25 30
Output60
Input1 3 5 7
Output0
🧠

How to Think About It

To find the sum of even numbers, go through each number one by one, check if it divides evenly by 2 using the remainder operator %, and if yes, add it to a running total. This way, you only add even numbers and ignore odd ones.
📐

Algorithm

1
Initialize a sum variable to zero.
2
Loop through each number in the given list or range.
3
Check if the current number is even by using the remainder operator.
4
If it is even, add it to the sum.
5
After the loop ends, output the sum.
💻

Code

bash
sum=0
for num in "$@"; do
  if (( num % 2 == 0 )); then
    ((sum+=num))
  fi
done
echo "$sum"
Output
6
🔍

Dry Run

Let's trace the input '1 2 3 4 5' through the code

1

Initialize sum

sum=0

2

Check number 1

1 % 2 = 1 (odd), sum remains 0

3

Check number 2

2 % 2 = 0 (even), sum = 0 + 2 = 2

4

Check number 3

3 % 2 = 1 (odd), sum remains 2

5

Check number 4

4 % 2 = 0 (even), sum = 2 + 4 = 6

6

Check number 5

5 % 2 = 1 (odd), sum remains 6

7

Output sum

Print 6

NumberIs Even?Sum After Step
1No0
2Yes2
3No2
4Yes6
5No6
💡

Why This Works

Step 1: Check even numbers

The script uses num % 2 == 0 to find even numbers because even numbers have no remainder when divided by 2.

Step 2: Add to sum

When a number is even, it is added to the sum variable using ((sum+=num)) which updates the total.

Step 3: Output result

After checking all numbers, the script prints the total sum of even numbers with echo.

🔄

Alternative Approaches

Using seq and awk
bash
seq 1 10 | awk '{if ($1 % 2 == 0) sum += $1} END {print sum}'
This uses external tools and is concise but less flexible for input arguments.
Using while loop and read
bash
sum=0
while read num; do
  if (( num % 2 == 0 )); then
    ((sum+=num))
  fi
done
echo "$sum"
Reads numbers from standard input line by line; useful for piped input.

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

Time Complexity

The script loops through each number once, so the time grows linearly with the number of inputs.

Space Complexity

Only a few variables are used regardless of input size, so space usage is constant.

Which Approach is Fastest?

The pure Bash loop is efficient and simple; using external tools like awk adds overhead but can be more concise.

ApproachTimeSpaceBest For
Bash loop with arithmeticO(n)O(1)Simple scripts with input arguments
seq with awkO(n)O(1)Quick one-liners with piped input
while read loopO(n)O(1)Reading from standard input or files
💡
Use ((num % 2 == 0)) to quickly check if a number is even in Bash.
⚠️
Forgetting to use double parentheses (( )) for arithmetic evaluation causes errors in Bash.