While Loop in Shell

Level 385 keystrokes

while loop continues executing as long as the condition is true.

Shell code

# Start the counter at 1
1count=1
# -le means less than or equal: the loop runs while count <= 5
2while [ $count -le 5 ]; do
# Prints Iteration: 1 up to Iteration: 5
3 echo "Iteration: $count"
# $(( )) does integer arithmetic; without this line the loop would never end
4 count=$((count + 1))
5done

Output

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
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.

  • $× 3
  • "× 2
  • (× 2
  • )× 2
  • =× 2
  • -× 1
  • ;× 1
  • :× 1
  • [× 1
  • ]× 1

More Shell examples at level 3

← Shell typing practice