DEV Community

Cover image for Bash is powerful! Lets build a simple number guesser game.
Humberto David Esquerra
Humberto David Esquerra

Posted on

Bash is powerful! Lets build a simple number guesser game.

Why Bash? and Why a number guesser?

A number guesser is a less complex project to build and you’ll learn how to work with if, else if, else statements, functions, user input, and the random module (Of course these programming concepts will be done the Bash way!). Wait but what is Bash? Bash is the default login shell for most linux systems. Meaning that to get good at linux it helps to get good at Bash. Bash can be ran straight from the linux terminal and can also be added to a text file and made executable using the linux command line on the spot! Also most (if not all) linux distros support bash so no need to download anything new. Just run it on the fly.

Alright lets build our game! Here’s the plan!

First we need a way to generate a random number (we’ll use the built in random function) and assign it to a variable that way we can reference the variable and compare it to the user input. Next we need to get user input. Then finally we need to compare the user input to the random number. If the user is guessed right then well end the game and let the user know they won! Else we need to continue the game until the user wins.

Alright simple enough, Lets start writing the code!

First we append the shebang (which is short for sharp and bang) and follow that with our /bin/bash path. We do this to ensure that our linux operating system executes the bash file properly. It should look like this.

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

Next we need a way to read user input through the command line. For this well use the built in read command. Well append that with -p to assign it to a variable (that way we can reference it later). This should look like this.

read -p "What number would you like to pick?" user_guess
Enter fullscreen mode Exit fullscreen mode

So read -p (read command), “what number would you like to pick” (the printed string), then user_guess (the variable name with assigned value).

Next we need to compare the user input to the random number to determine whether or not the game player is right. For this well use if/else statements. If/else statements check if a condition is true and will execute a code block depending on condition. In bash this logical statements looks like this.

if [conditional] then (do something here) else (do something) fi
Enter fullscreen mode Exit fullscreen mode

We also want to print things to the terminal to let the user know whether or not hes correct. For this well use echo. Echo (actually echo lowercase) is another built in command and is used to print strings to the terminal. Echo looks like this.

echo "this string will be printed to the terminal screen"
Enter fullscreen mode Exit fullscreen mode

So combining echo and our if statement, our actual code should look like this.

if [ ${random_number} == ${user_guess} ]
then
    echo "You guessed right!"
else 
    echo "You guessed wrong"
fi
Enter fullscreen mode Exit fullscreen mode

Awesome! Lets build the final product!

Now that we have the bare bones logic in order, from here i’ll clean up the code a little and make it more readable. I’ll also add some logic to a function and call that function because (in my opinion) it makes everything more readable, But more importantly I will be using recursion to recall the function if the current user guesses wrong. So here’s the finished product!

echo "Random number guesser!"                                                                       

read -p "What is the random number you would like to guess? Please    
         pick a number between 0 and 9" user_guess

function number_guess() {                                                                            
   if [ ${random_number} == ${user_guess} ]                                                        
   then                                                                                            
       echo "You guessed right! Congratulations ${random_number} is 
             equal to ${user_guess}"        
   else                                                                                            
        echo "You guessed wrong! Try again."                                                        
        read -p "What is the random number you would like to guess? 
                please pick a number between 0 and 9" user_guess
        number_guess                                                                                
    fi                                                                                              
}                                                                                                   

number_guess
Enter fullscreen mode Exit fullscreen mode

As you can see building a simple number guessing game isn’t super difficult. It just takes a few lines of code and because we utilized bash this script is super easy to run from the command line.

Thank you for reading my article!

Wondering what i’m up to? You can check me out here at http://davidesquerra.com/ or here at https://github.com/Davidfree2

If you really enjoyed this article drop a clap down below or even a follow! Let me know what you think and if you want to see more content like this. Drop a question too if there’s something that’s not clear enough. Thank you for reading.

Oldest comments (2)

Collapse
 
bobbyiliev profile image
Bobby Iliev

That is very cool! If you are looking for a project to contribute to for Hackrtoberfest, it would be great to have this as a chapter in the Introduction to Bash Scripting eBook:

GitHub logo bobbyiliev / introduction-to-bash-scripting

Free Introduction to Bash Scripting eBook

💡 Introduction to Bash Scripting

This is an open-source introduction to Bash scripting guide/ebook that will help you learn the basics of Bash scripting and start writing awesome Bash scripts that will help you automate your daily SysOps, DevOps, and Dev tasks. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Bash scripts to combine different Linux commands and automate boring and repetitive daily tasks, so that you can focus on more productive and fun things.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Bash scripting.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters

The first 13 chapters would be purely focused on getting some solid Bash scripting foundations then the rest of…

Collapse
 
davidfree2 profile image
Humberto David Esquerra

Yes! That sounds great lets keep in touch.