Arithmetic Operations in Shell

Level 3112 keystrokes

$(( )) evaluates integer arithmetic. With a=10 and b=3 it prints the sum, product, integer quotient (3, the fraction is dropped) and remainder (1).

Shell code

# Two assignments on one line: a=10 and b=3
1a=10 b=3
# Inside $(( )) variables need no $: 10 + 3 = 13
2echo "Sum: $((a + b))"
# Multiplication: 30
3echo "Product: $((a * b))"
# Integer division drops the fraction: 10 / 3 = 3
4echo "Division: $((a / b))"
# % gives the remainder: 10 % 3 = 1
5echo "Modulo: $((a % b))"

Output

Sum: 13
Product: 30
Division: 3
Modulo: 1
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • "× 8
  • (× 8
  • )× 8
  • :× 4
  • $× 4
  • =× 2
  • *× 1
  • /× 1
  • %× 1
  • +× 1

More Shell examples at level 3

← Shell typing practice