Arithmetic Operations in Shell

Level 4144 keystrokes

$(( )) evaluates an integer expression and inserts the result, while (( )) evaluates it without output, for example to increment a variable. a=10 and b=3 give 13, 30, 3, 1, and then a becomes 11.

Shell code

# ; separates two assignments on one line
1a=10; b=3
# $(( )) inserts the result: 13
2echo "Sum: $((a + b))"
# 10 * 3 = 30
3echo "Product: $((a * b))"
# Integer division, the fraction is dropped: 3
4echo "Division: $((a / b))"
# Remainder of 10 / 3: 1
5echo "Modulo: $((a % b))"
# (( )) evaluates without printing; a++ changes a to 11
6((a++))
7echo "Incremented: $a"

Output

Sum: 13
Product: 30
Division: 3
Modulo: 1
Incremented: 11
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.

  • "× 10
  • (× 10
  • )× 10
  • :× 5
  • $× 5
  • +× 3
  • =× 2
  • ;× 1
  • *× 1
  • /× 1

More Shell examples at level 4

← Shell typing practice