Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • End of 10 Events
    • Wake Forest, NC, — 2025-09-20
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Learn Bash by writing a number guessing game
  • Bash
  • Fun
  • Programming

Learn Bash by writing a number guessing game

Learning a new programming language can be a fun exercise.
Jim Hall April 14, 2024 5 minutes read
The Bash logo

The Bash logo

Whenever I try to learn a new language, whether it’s a new shell scripting language or a new programming language to write larger programs, I focus on a few things: defining variables, writing statements, and evaluating expressions. Once I understand how to do those concepts, I can usually figure out the rest on my own. Most programming languages share some similarities, so learning the rest is usually a matter of working out the details.

I like to write a few “test” or “scratch” programs to practice a new programming language. One sample program I use frequently as an example is the classic “guess the number” program, where the computer picks a number in a range and prompts me to guess the secret number. This exercises how to assign values, how to write statements, and how to evaluate and compare values.

If you’d like to learn how to write shell scripts in Bash, you can use the “guess the number” program to practice Bash scripting.

A brief introduction to Bash

GNU Bash is the standard shell for most Linux system. Its name comes from the original Unix shell, which was just named sh. Later, Unix added support for other shells, including ksh for the Korn Shell, and bsh for the Bourne Shell. Bash is actually short for the “Bourne Again Shell,” a play on the original “Bourne Shell” name on older “Big Unix” systems.

Bash supports all the features of bsh but adds several other useful features. Its backwards compatibility with classic Unix shells makes it a popular choice on many Linux systems.

Guess the number in Bash

To write a “guess the number” script in Bash, we need to do a few things:

  1. Pick a random number from 1 to 100, and save it to a variable (secret)
  2. Enter a loop where the user enters their guess, and the script says if the guess is too high or too low
  3. Exit the loop when the user guesses the secret number

To select a random value, you can use the $RANDOM internal variable. The name of the variable is actually RANDOM and you can reference the value of the variable with the dollar sign in front: $RANDOM will always generate a random number, usually a large number.

You can use the “modulo” arithmetic operator (%) to divide the random number by another number and get the remainder. For example, 21 % 10 is 1, because 21 divided by 10 is 2, with 1 left over. But 20 % 10 is 0, because 10 goes into 20 exactly twice, with nothing left over. That means $RANDOM % 100 will give values between zero and 99. To make the secret number between 1 and 100, use $RANDOM % 100 + 1.

Bash provides arithmetic expansion using the $(( )) notation, so use this to calculate a random number from 1 to 100 and save it in a new variable called secret:

secret=$(( $RANDOM % 100 + 1 ))

Next, set a variable to store the user’s guess. We can initialize it to zero with:

guess=0

To start a loop that will continue only if the user’s guess is not equal to the secret number, use:

while [ "0$guess" -ne $secret ] ; do
...
done

In this example, I’ve used "0$guess to prepend a zero in front of the user’s guess, in case the user types a non-digit value like m. The -ne comparison test will only work with numbers, so 0m -ne 50 will test what we want: that the number value of 0 is not equal to 50, if the secret number was 50.

Get values from the user with the read statement:

read guess

You can test if one number is less than another using the -lt comparison, and greater than using the -gt test. You can use an if block in Bash to do each test, but a one-line shortcut uses && to basically say “if this test on the left is true then do this task on the right. For example, you could add two tests to print if the user’s guess was too low or too high:

[ "0$guess" -lt $secret ] && echo "Too low"
[ "0$guess" -gt $secret ] && echo "Too high"

Putting it all together

Let’s combine what we’ve learned about Bash scripting in this file, called guess:

#!/bin/bash

secret=$(( $RANDOM % 100 + 1 ))

guess=0
echo "Guess a number between 1 and 100"

while [ "0$guess" -ne $secret ] ; do
    read guess
    [ "0$guess" -lt $secret ] && echo "Too low"
    [ "0$guess" -gt $secret ] && echo "Too high"
done

echo "That's right!"

I’ve added an echo statement at the top to tell the user what the script will do, and another echo statement at the end to let the user know when they’ve guessed the number.

Make this script executable with the chmod command:

$ chmod +x guess

And now you can run your script whenever you want to play the “guess the number” game:

$ ./guess
Guess a number between 1 and 100
50
Too high
25
Too low
33
Too high
30
Too high
28
Too high
27
That's right!

Every time you run the script, the $RANDOM internal variable gives a new number, so you’ll always have a random secret number between 1 and 100:

$ ./guess
Guess a number between 1 and 100
50
Too low
75
Too low
88
Too high
80
Too low
84
That's right!

Learning is fun

This “guess the number” game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare details in each language.

Tags: Bash Fun Linux Programming

Post navigation

Previous: Temporary fixes that become permanent
Next: Use rclone to put your files in the cloud

Related Stories

command_line_prompt
  • Command Line
  • Linux
  • Programming

Writing a replacement seq command

Jim Hall April 27, 2026
BUSINESS_twoforward
  • Fun
  • LibreOffice

Is there a better Pi Day than March 14?

Jim Hall March 14, 2026
A Stylized image of a Raspberry on a blue background
  • Fun
  • Raspberry Pi

Let’s measure pi with a Raspberry Pi

Jim Hall March 13, 2026

System upgrades this Sunday, May 3

Tools illustrationFedora 44 was released this week and I’ve upgraded all my systems except for the two that directly affect Both.org. I’ll be upgrading the hosts for my server and firewall to Fedora 44 this Sunday afternoon, May 3.

Both.org will be down for most of the afternoon for these upgrades.

Thanks for your patience.

Random Quote

The value of a program is proportional to the weight of its output.

— Laws of Computer Programming

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.