Menu
Coddy logo textTech

Recap - Number Guesser

Part of the Fundamentals section of Coddy's Terminal journey — lesson 73 of 82.

challenge icon

Challenge

Easy

Create a shell script called guesser.sh that simulates a number guessing game with a fixed sequence of guesses.

Your script should:

  1. Start with the shebang line (#!/bin/bash)
  2. Set a variable called secret with the value 5
  3. Set a variable called guess with the initial value 0
  4. Use a while loop that continues as long as guess does not equal secret
  5. Inside the loop, increment guess by 1
  6. Use an if statement to check the guess:
    • If guess is less than secret, print: Guess [guess]: Too low!
    • If guess equals secret, print: Guess [guess]: Correct!

Make the script executable and run it. Your output should be:

Guess 1: Too low!
Guess 2: Too low!
Guess 3: Too low!
Guess 4: Too low!
Guess 5: Correct!

Hint: Use -ne for "not equal" in the while condition, -lt for "less than" in the if statement, and -eq for "equal to". Increment the guess at the start of each loop iteration with guess=$((guess + 1)).

Try it yourself

Terminal

All lessons in Fundamentals